多线程基础(六):Object的wait方法以及notify与notifyAll的区别

本文深入探讨Java并发编程中notify与notifyAll方法的差异,通过生产者消费者模型展示两者如何影响线程唤醒机制,揭示死锁产生的原因及解决策略。

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


还记得前面用ArrayList实现阻塞队列的文章:《 什么?面试官让我用ArrayList实现一个阻塞队列?》。我们通过synchronized并配合wait和notify实现了一个阻塞队列。在介绍完前文的synchronized关键字的基本使用之后,本文来对这些方法进行分析。

1.生产者和消费者模型

Producer代码如下:

public class Producer implements Runnable {

	List<Integer> cache;

	public Producer(List<Integer> cache) {
		new Object();
		this.cache = cache;
	}

	public void put() throws InterruptedException {
		synchronized (cache) {
			System.out.println(Thread.currentThread().getName()+"获得锁");
			cache.notify();
			while (cache.size() == 1) {
				try {
					System.out.println(Thread.currentThread().getName()+"wait");
					cache.wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}

			TimeUnit.SECONDS.sleep(1);
			cache.add(1);
			System.out.println(Thread.currentThread().getName() + "生产者写入1条消息");
		}

	}


	@Override
	public void run() {
		while (true) {
			try {
				put();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}

Consumer代码如下:


public class Consumer implements Runnable {


	List<Integer> cache;

	public Consumer(List<Integer> cache) {
		this.cache = cache;
	}

	private void consumer() {
		synchronized (cache) {
			System.out.println(Thread.currentThread().getName()+"获得锁");
			cache.notify();
			while (cache.size() == 0) {
				try {
					System.out.println(Thread.currentThread().getName()+"wait");
					cache.wait();
				}catch (InterruptedException e) {
					e.printStackTrace();
				}
			}

			cache.remove(0);
			System.out.println(Thread.currentThread().getName()+" 消费者消费了1条消息");
		}
	}

	@Override
	public void run() {
		while (true) {
			consumer();
		}
	}
}

我们来调用上述的生产者和消费者模型:

public static void main(String[] args) {
	List<Integer> cache = new ArrayList<>();
	new Thread(new Producer(cache),"P1").start();
	new Thread(new Consumer(cache),"C1").start();
}

启用了两个线程,分别调用生产者和消费者,可以看到这个过程交替执行:

P1获得锁
P1生产者写入1条消息
P1获得锁
P1wait
C1获得锁
C1 消费者消费了1条消息
C1获得锁
C1wait
P1生产者写入1条消息
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值