java 线程同步例子(zt)

    package test_1;  
      
    public class BigSmallMonk  
    {  
        public static void main(String[] args)  
        {  
            WaterIntake Omonk=new WaterIntake();  
            PouringWater Ymonk=new PouringWater();  
            Thread Ymonk1=new Thread(Ymonk);  
            Ymonk1.setName("Ymonk1");  
            Thread Omonk1=new Thread(Omonk);  
            Omonk1.setName("Omonk1");  
            Ymonk1.start();  
            Omonk1.start();  
        }  
      
    }  
    class WaterTank//水缸  
    {  
        static int num=30;//水缸所能承受水的桶数  
        static int currentNum=0;//水缸里现有的水的桶数  
        static boolean flag = true;//缸口是否有人使用  
        public void reduce()//取水  
        {  
            synchronized (this)  
            {  
                while (!flag)  
                {  
                    try  
                    {  
                        System.out.println("缸口有人使用");  
                        this.wait();  
                    } catch (InterruptedException e)  
                    {  
                        // TODO Auto-generated catch block  
                        e.printStackTrace();  
                    }  
                }  
                //这里不需要设置false,因为你这样一设置,另外一个线程总是执行那个while里并且在wait了。
                //flag=false;  
                try  
                {  
                    Thread.sleep(1000);  
                } catch (InterruptedException e)  
                {  
                    // TODO Auto-generated catch block  
                    e.printStackTrace();  
                }  
                currentNum--;  
                flag=true;  
                this.notify();  
                System.out.println(Thread.currentThread().getName()+"取出了水");  
            }  
        }  
        public void push()//倒水  
        {  
            synchronized (this)  
            {  
                while (!flag)  
                {  
                    try  
                    {  
                        System.out.println("缸口有人使用");  
                        this.wait();  
                    } catch (InterruptedException e)  
                    {  
                        // TODO Auto-generated catch block  
                        e.printStackTrace();  
                    }  
                }
                //这里不需要设置false,因为你这样一设置,另外一个线程总是执行那个while里并且在wait了。  
                //flag=false;  
                try  
                {  
                    Thread.sleep(1000);  
                } catch (InterruptedException e)  
                {  
                    // TODO Auto-generated catch block  
                    e.printStackTrace();  
                }  
                currentNum++;  
                flag=true;  
                this.notify();  
                System.out.println(Thread.currentThread().getName()+"倒入了一桶水");  
            }  
              
        }  
    }  
    class WaterIntake implements Runnable//取水线程  
    {  
        WaterTank water=new WaterTank();  
        public void run()  
        {  
            while (true)  
            {  
                water.reduce();  
            }  
        }  
    }  
    class PouringWater implements Runnable//倒水线程  
    {  
        WaterTank water=new WaterTank();  
        public void run()  
        {     
            while(true)  
            {  
                water.push();  
            }  
        }  
          
    }  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值