多线程同步(Lock)

实例

模仿生产者和消费者的模式

 

先来个两个线程的

class ProducerConsumerDemo
{
	public static void main(String[] args)
	{
		Res r=new Res();
		Pro in=new Pro(r);
		Cou out=new Cou(r);
		Thread t1=new Thread(in);
		Thread t2=new Thread(out);
		t1.start();
		t2.start();
	}
}
class Res
{
	private String name;
	private int count=1;
	private boolean flag=false;
	public synchronized void set(String name)
	{
		if(flag)
			try{wait();}catch(Exception e){}
		this.name=name+"---"+count++;
			System.out.println(Thread.currentThread().getName()+"+++生产者+++"+this.name);
		flag=true;
		this.notify();
	}
	public synchronized void out()
	{
		if(!flag)
			try{wait();}catch(Exception e){}
			System.out.println(Thread.currentThread().getName()+"消费者"+this.name);
		flag=false;
		this.notify();
	}
}
class Pro implements Runnable
{
	private Res r;
	Pro(Res r)
	{
		this.r=r;
	}
	public void run()
	{
		while(true)
		{
			r.set("--商品--");
		}
	}
}
class Cou implements Runnable
{
	private Res r;
	Cou(Res r)
	{
		this.r=r;
	}
	public void run()
	{
		while(true)
		{
			r.out();
		}
	}
}

 

 

这样就OK了

 

下面就试试四个线程的,两个线程生产,两个线程消费;

class ProducerConsumerDemo
{
	public static void main(String[] args)
	{
		Res r=new Res();
		Pro in=new Pro(r);
		Cou out=new Cou(r);
		Thread t1=new Thread(in);
		Thread t2=new Thread(in);
		Thread t3=new Thread(out);
		Thread t4=new Thread(out);
		t1.start();
		t2.start();
		t3.start();
		t4.start();
	}
}
class Res
{
	private String name;
	private int count=1;
	private boolean flag=false;
	public synchronized void set(String name)
	{
		if(flag)
			try{wait();}catch(Exception e){}
		this.name=name+"---"+count++;
			System.out.println(Thread.currentThread().getName()+"+++生产者+++"+this.name);
		flag=true;
		this.notify();
	}
	public synchronized void out()
	{
		if(!flag)
			try{wait();}catch(Exception e){}
			System.out.println(Thread.currentThread().getName()+"消费者"+this.name);
		flag=false;
		this.notify();
	}
}
class Pro implements Runnable
{
	private Res r;
	Pro(Res r)
	{
		this.r=r;
	}
	public void run()
	{
		while(true)
		{
			r.set("--商品--");
		}
	}
}
class Cou implements Runnable
{
	private Res r;
	Cou(Res r)
	{
		this.r=r;
	}
	public void run()
	{
		while(true)
		{
			r.out();
		}
	}
}


但结果是多生产少消费,并不是我们想要的一生产一消费;

 

改良代码

class ProducerConsumerDemo
{
	public static void main(String[] args)
	{
		Res r=new Res();
		Pro in=new Pro(r);
		Cou out=new Cou(r);
		Thread t1=new Thread(in);
		Thread t2=new Thread(in);
		Thread t3=new Thread(out);
		Thread t4=new Thread(out);
		t1.start();
		t2.start();
		t3.start();
		t4.start();
	}
}
class Res
{
	private String name;
	private int count=1;
	private boolean flag=false;
	public synchronized void set(String name)
	{
		while(flag)
			try{wait();}catch(Exception e){}
		this.name=name+"---"+count++;
			System.out.println(Thread.currentThread().getName()+"+++生产者+++"+this.name);
		flag=true;
		this.notifyAll();
	}
	public synchronized void out()
	{
		while(!flag)
			try{wait();}catch(Exception e){}
			System.out.println(Thread.currentThread().getName()+"消费者"+this.name);
		flag=false;
		this.notifyAll();
	}
}
class Pro implements Runnable
{
	private Res r;
	Pro(Res r)
	{
		this.r=r;
	}
	public void run()
	{
		while(true)
		{
			r.set("--商品--");
		}
	}
}
class Cou implements Runnable
{
	private Res r;
	Cou(Res r)
	{
		this.r=r;
	}
	public void run()
	{
		while(true)
		{
			r.out();
		}
	}
}


对于多个生产者和消费者。

为什么要定义while判断标记。

原因:让被唤醒的线程在一次判断标记。

 

为什么定义notifyAll?

因为需要唤醒对方线程。

以为只用notify,容易出现只唤醒本方线程的情况,导致程序中的所有线程都等待。

 

 

JDK新版中提供了多线程升级解决方案。
将同步Syschronized替换成现实Lock操作;
将Object中的wait, notify notifyAll,替换了Condition对象。
该对象可以Lock锁 进行获取。
实现了本方只唤醒对方的操作;

import java.util.concurrent.locks.*;
class ProducerConsumerDemo2
{
	public static void main(String[] args)
	{
		Res r=new Res();
		Pro in=new Pro(r);
		Cou out=new Cou(r);
		Thread t1=new Thread(in);
		Thread t2=new Thread(in);
		Thread t3=new Thread(out);
		Thread t4=new Thread(out);
		t1.start();
		t2.start();
		t3.start();
		t4.start();
	}
}
class Res
{
	private String name;
	private int count=1;
	private boolean flag=false;

	private Lock  lock=new ReentrantLock();
	private Condition  condition_in=lock.newCondition();
	private Condition  condition_out=lock.newCondition();

	public void set(String name)throws InterruptedException
	{
		lock.lock();
		try
		{
			while(flag)
				condition_in.await();
			this.name=name+"---"+count++;
				System.out.println(Thread.currentThread().getName()+"+++生产者+++"+this.name);
			flag=true;
			condition_out.signal();
		}
		finally
		{
			lock.unlock();//解锁对象一定要执行
		}
	}
	public void out()throws InterruptedException
	{
		lock.lock();
		try
		{
			while(!flag)
				condition_out.await();
				System.out.println(Thread.currentThread().getName()+"消费者"+this.name);
			flag=false;
			condition_in.signal();
		}
		finally
		{
			lock.unlock();
		}
		
	}
}
class Pro implements Runnable
{
	private Res r;
	Pro(Res r)
	{
		this.r=r;
	}
	public void run()
	{
		try
		{
			while(true)
			{
				r.set("--商品--");
			}
		}
		catch (InterruptedException e)
		{
		}
		
	}
}
class Cou implements Runnable
{
	private Res r;
	Cou(Res r)
	{
		this.r=r;
	}
	public void run()
	{
		try
		{
			while(true)
			{
				r.out();
			}
		}
		catch (InterruptedException e)
		{
		}
	}
}


 

 


 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值