thread15.java

来自「学习java编程的好程序」· Java 代码 · 共 169 行

JAVA
169
字号
class ham
{ 
    static Object box1=new Object(); 
    static Object box2=new Object(); 
    static int totalmaterial1=10; 
    static int totalmaterial2=10;
    static int sales11=0; 
    static int sales12=0; 
    static int sales21=0;
    static int sales22=0; 
    static int production1=5; 
    static int production2=5; 
}
class hmaker1 extends Thread 
{ 
    public void make() 
    { 
        synchronized(ham.box1) 
        { 
            (ham.production1)++; 
            System.out.println("厨师"+getName()+":"+"汉堡来了(总共"+(ham.production1-ham.sales11-ham.sales12)+"个A类汉堡)");  
        } 
    try 
    { 
        ham.box1.notify(); 
    } 
    catch(Exception e){} 
    } 
    public void run() 
    { 
        while(ham.production1<ham.totalmaterial1) 
        { 
            make();   
            try 
            { 
                sleep(3000); 
            } 
            catch(Exception e){} 
        } 
        if(ham.production1==ham.totalmaterial1)
        {
		        System.out.println("所有的材料用完了!");
        } 
    } 
} 

class hmaker2 extends Thread 
{ 
    public void make() 
    { 
        synchronized(ham.box2) 
        { 
            (ham.production2)++; 
            System.out.println("厨师"+getName()+":"+"汉堡来了(总共"+(ham.production2-ham.sales21-ham.sales22)+"个B类汉堡)");  
        } 
        try 
        { 
            ham.box2.notify(); 
        }        catch(Exception e){} 
    } 
    public void run() 
    { 
        while(ham.production2<ham.totalmaterial2) 
        {        
            make(); 
            try 
            { 
                sleep(3000); 
            } 
            catch(Exception e){} 
        }   
        if(ham.production2==ham.totalmaterial2)
        {
             System.out.println("所有的材料用完了!");
        } 
    } 
} 
class hassistant extends Thread 
{ 
    public void sell1() 
    { 
        if(ham.production1==(ham.sales11+ham.sales12)) 
        { 
            System.out.println("营业员"+getName()+":顾客朋友们,请稍微等一下,A型汉堡没了!"); 
            ham.sales11=0;
            ham.sales12=0;
            ham.production1=0;
            try             
            {           
                ham.box1.wait(); }
            catch(Exception e){} 
        }   
        else
        {  
            if(ham.production1>(ham.sales11+ham.sales12))
            {               ham.sales11++; 
                ham.sales21++; 
                System.out.println("营业员"+getName()+":顾客好,汉堡上来了,(总共卖了 A类汉堡"+ham.sales11+"个),总共卖了B类汉堡"+ham.sales21+"个)");            
            }
        }  
    }
 
    public void sell2() 
    { 
        if(ham.production2==(ham.sales21+ham.sales22)) 
        { 
            System.out.println("营业员"+getName()+":顾客朋友们,请稍微等一下,B型汉堡没了!"); 
            ham.sales21=0;
            ham.sales22=0;
            ham.production2=0; 
            try 
            {
                ham.box2.wait(); 
            } 
            catch(Exception e){} 	  
        } 
        else
        {   
            if(ham.production2>(ham.sales21+ham.sales22))
            {                                                               
                ham.sales12++;  
                ham.sales22++; 
                System.out.println("营业员"+getName()+":顾客好,汉堡上来了,(总共卖了A类汉堡"+ham.sales12+"个,总共卖了B类汉堡"+ham.sales22+"个"); 
            }
        } 
    } 
    public void run() 
    { 
        while((ham.sales12+ham.sales11)<ham.production1) 
        { 
            try 
            { 
                sleep(1000);
            } 
            catch(Exception e){} 
        sell1(); 
        } 
        while((ham.sales12+ham.sales22)<ham.production2)
        { 
            try 
            { 
                sleep(2000); 
            } 
            catch(Exception e){} 
            sell2(); 
        }
        System.out.println("还剩下A类汉堡:"+(ham.production1-ham.sales11-ham.sales12)+"个");   
        System.out.println("还剩下B类汉堡:"+(ham.production2-ham.sales21-ham.sales22)+"个"); 
    } 
} 
public class thread15 
{ 
    public static void main(String[] args) 
    { 
        hmaker1 maker1=new hmaker1(); 
        hmaker2 maker2=new hmaker2(); 
        hassistant assistant1=new hassistant(); 
        hassistant assistant2=new hassistant(); 
        maker1.setName("甲"); 
        maker2.setName("乙");
        assistant1.setName("甲"); 
        assistant2.setName("乙"); 
        maker1.start(); 
        maker2.start(); 
        assistant1.start();     
        assistant2.start();
    } 
} 

⌨️ 快捷键说明

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