线程中JDK1.5版本后的LOCK用法

需求:实现生产者和消费者,两个生产者两个消费者

class Resource
{
	private String name;
	private int count=1;
	boolean flag=false;
	private Lock lock=new ReentrantLock() ;
	private Condition condition_pro =lock.newCondition() ;
	private Condition  condition_cus=lock.newCondition() ;
	public  void setName(String name)throws InterruptedException
	{
		lock.lock();
		try{
		while(flag)								
		condition_pro.await();
		this.name=name+"---"+count++;
				System.out.println(Thread.currentThread()+"生产"+this.name);
			flag=true;
		condition_cus.signal() ;
		
		}catch (InterruptedException e1){System.out.println(e1.toString());}

			finally{
				lock.unlock() ;

			}
			//this.notify();
		
/*
原因:t1先抢到执行权,判断falg为假,执行生产打印出"生产6786",然后将flag置为true,t2进行判断flag为true则停在wait()处放弃资格。t3获取执行权执行一次消费打印出“消费6786”,
这是正常模式。然后t3将flag置为false,t4进行判断flag为假则停在wait()处。这个时候t1有可能再次抢到执行权,判断flag为假,直接进行生产,生产后直接唤醒所有线程,
因为t2是最先在线程池中等待的线程,所以将会第一个被唤醒,t2被唤醒后将不会去判断flag条件,而是直接进行生产,这就是造成生产两次的原因。
*/		
		
	}
	public  void out()throws InterruptedException 
	{
		lock.lock();
		try{
			while(!flag)
		condition_cus.await();
		System.out.println(Thread.currentThread()+"消费------"+this.name);
			flag=false;
			condition_pro.signal();
		
		
		}catch (InterruptedException e2){System.out.println(e2.toString());}
		finally
		{
		lock.unlock();
		
		}
			
	
		
	}
		
	
	
}
class Produce implements Runnable
{
	private Resource r;
	Produce(Resource r)
	{
		this.r=r;
	}
	public void run()
	{
		while (true)
		{
			try
			{
				r.setName("商品");
			}
			catch (InterruptedException e3)
			{
				System.out.println(e3.toString());
			}
			
		}
		
	}
}
class Custom implements Runnable
{
	private Resource r;
	Custom(Resource r)
	{
		this.r=r;
	}
	public void run()
	{
		while (true)
		{
			try
				{
					r.out();
				}
				catch (InterruptedException e4)
				{
					System.out.println(e4.toString());
				}
			
		}
	
	}
}
class DeadLock
{
	public static void main(String[] args)
	{
		Resource r=new Resource();
		Produce p=new Produce(r);
		Custom c=new Custom(r);
		Thread t1=new Thread(p);
		Thread t2=new Thread(p);
		Thread t3=new Thread(c);
		Thread t4=new Thread(c);
		t1.start();
		t2.start();
		t3.start();
		t4.start();
	
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值