多线程同步中的wait,notify,notifyAll方法的深入学习

本文介绍Java多线程中生产者消费者模式的实现细节,包括如何使用wait、notify和notifyAll方法来协调生产者和消费者的同步问题,并讨论了在实际应用中可能遇到的问题及解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在多线程的运用中,难免希望某个线程暂停或唤醒某个线程,比如希望操作不同功能的线程交替执行。因此,java提供了wait,notify,和notifyAll方法去实现该效果。

wait():用于冻结执行该语句的线程,使其失去执行权,wait方法返回值是异常,所以要try,线程被冻结后,同步对其失效,也就是说该线程在同步代码中冻结了,其他线程可以进入同步代码。

notify():用于唤醒所有处于被冻结状态的线程中最早被冻结的线程。

notifyAll():用于唤醒所有处于冻结状态的线程。

要注意的是,调用方法是通过“锁”对象去点调用这些方法,而且作用的线程局限于被该“锁“控制的线程,而不是程序中所有线程。

实例解释:

//需求:产品每生产一个消费一个
class ProducerConsumerDemo 
{
	public static void main(String[] args) 
	{
		Resource r = new Resource();
		Producer pro = new Producer(r);
		Consumer con = new Consumer(r);
		Thread t1 = new Thread(pro);
		Thread t2 = new Thread(pro);
		Thread t3 = new Thread(con);
		Thread t4 = new Thread(con);
		t1.start();
		t2.start();
		t3.start();
		t4.start();

	}
}
class Resource
{
	private String name;
	private int count = 1;
	private boolean flag = false;
	public synchronized void set(String name)
	{
		if(flag)
			try{this.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 Producer implements Runnable
{
	private Resource res;

	Producer(Resource res)
	{
		this.res = res;
	}
	public void run()
	{
		while(true)
		{
			res.set("+商品+");
		}
	}
}

class Consumer implements Runnable
{
	private Resource res;

	Consumer(Resource res)
	{
		this.res = res;
	}
	public void run()
	{
		while(true)
		{
			res.out();
		}
	}
}
但是结果却不尽如人意:


如图所示,同一商品被卖了两次,这是为什么呢?

原因是,在执行out方法时,如果有一个线程被冻结了,那么当它被唤醒时,它不会再判断flag,而直接执行后面语句,从而导致该错误,同样,改程序有可能出现生产者生产两个产品去没人买。

要想解决该问题,只能让线程被唤醒后,要重新判断flag,因此把if语句改成while

//需求:产品每生产一个消费一个
class ProducerConsumerDemo 
{
	public static void main(String[] args) 
	{
		Resource r = new Resource();
		Producer pro = new Producer(r);
		Consumer con = new Consumer(r);
		Thread t1 = new Thread(pro);
		Thread t2 = new Thread(pro);
		Thread t3 = new Thread(con);
		Thread t4 = new Thread(con);
		t1.start();
		t2.start();
		t3.start();
		t4.start();

	}
}
class Resource
{
	private String name;
	private int count = 1;
	private boolean flag = false;
	public synchronized void set(String name)
	{
		while(flag)
			try{this.wait();}catch(Exception e){}
		this.name = name+"--"+count++;
		System.out.println(Thread.currentThread().getName()+"...生产者.."+this.name);
		flag = true;
		this.notify();
	}
	public synchronized void out()
	{
		while(!flag)
			try{wait();}catch(Exception e){}
		System.out.println(Thread.currentThread().getName()+"...消费者........."+this.name);
		flag = false;
		this.notify();
	}
}

class Producer implements Runnable
{
	private Resource res;

	Producer(Resource res)
	{
		this.res = res;
	}
	public void run()
	{
		while(true)
		{
			res.set("+商品+");
		}
	}
}

class Consumer implements Runnable
{
	private Resource res;

	Consumer(Resource res)
	{
		this.res = res;
	}
	public void run()
	{
		while(true)
		{
			res.out();
		}
	}
}
结果很遗憾,比上次更惨,中途直接停住了:


这是因为,改为while后,可能会出现线程全都冻结的状况。。。。(t1执行set,再执行一次就冻结了,t2执行set,冻结了,t3执行out,t1解冻,t3再执行,冻结了,t4执行out,冻结了,t1执行set,t2解冻,t1再执行set,冻结了,t2执行set,冻结了,这时全部冻结)

为了排除全部冻结的情况,应该把notify改为notifyAll

//需求:产品每生产一个消费一个
class ProducerConsumerDemo 
{
	public static void main(String[] args) 
	{
		Resource r = new Resource();
		Producer pro = new Producer(r);
		Consumer con = new Consumer(r);
		Thread t1 = new Thread(pro);
		Thread t2 = new Thread(pro);
		Thread t3 = new Thread(con);
		Thread t4 = new Thread(con);
		t1.start();
		t2.start();
		t3.start();
		t4.start();

	}
}
class Resource
{
	private String name;
	private int count = 1;
	private boolean flag = false;
	public synchronized void set(String name)
	{
		while(flag)
			try{this.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 Producer implements Runnable
{
	private Resource res;

	Producer(Resource res)
	{
		this.res = res;
	}
	public void run()
	{
		while(true)
		{
			res.set("+商品+");
		}
	}
}

class Consumer implements Runnable
{
	private Resource res;

	Consumer(Resource res)
	{
		this.res = res;
	}
	public void run()
	{
		while(true)
		{
			res.out();
		}
	}
}
结果正确!!

文章中实例来自网络



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值