saleslady.java

来自「通过买票这个简实例来介绍java线程的一些应用」· Java 代码 · 共 36 行

JAVA
36
字号
public class SalesLady { 
int memontoes, five, ten; 

    public synchronized String ruleForSale(int num, int money) { 
        String s = null; 
        if (memontoes <= 0) 
            return "对不起,已经售完"; 
        if (money == 5) { 
            memontoes--; 
            five++; 
            s = "给你票," + "你的钱正好。"; 
        } else if (money == 10) { 
            while (five < 1) { 
                try { 
                    System.out.println("" + num + "号顾客用10元购票,请等待"); 
            	wait(); 
                } catch (InterruptedException e) { 
                } 
            } 
            memontoes--; 
            five -= 1; 
            ten++; 
            s = "给你票," + "找你5元。"; 
        } 
        //notifyAll(); 
        return s; 
    } 

    SalesLady(int m, int f, int t) { 
        memontoes = m; 
        five = f; 
        ten = t; 
    } 

} 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?