多线程之经典生产者消费者问题

本文介绍了一个使用Java实现的生产者消费者问题解决方案。通过wait和notify方法控制生产者和消费者线程间的同步,确保每次只有一个线程操作资源。

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

通过信号量,wait,nofity,synchronized来解决生产者消费者问题。

实例如下:


package thread;

public class ThreadWaitAndNotifyTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Iphone iphone = new Iphone();
		Thread t1 = new Thread(new Producer(iphone));
		Thread t2 = new Thread(new Salesman(iphone));
		t1.start();
		t2.start();
	}

}

class Iphone {
	// Iphone是否已卖掉
	private boolean isSaled = true;
	private String name;
	
	public synchronized void  setName(int i) {
		System.out.println(Thread.currentThread().getName()+"正在生产第"+(i)+"部IPhone...");
		if (!isSaled) {
			System.out.println(Thread.currentThread().getName()+"第" + (i-1)+ "部IPhone等待被卖掉。");
			try {
				wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
//			System.out.println(Thread.currentThread().getName()+"第" + (i-1)+ "部IPhone已被卖掉。");
		}
		
		this.name = "Iphone 5S("+i+")";
		this.isSaled = false;
		System.out.println(Thread.currentThread().getName()+"已生产第"+(i)+"部IPhone...");
		notify(); // 通知消费者线程已生产IPhone,等待被卖掉。
	}
	
	public synchronized String getName(int i) {
		if (isSaled) {
			System.out.println(Thread.currentThread().getName()+"wait for sale start...");
			try {
				wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println(Thread.currentThread().getName()+"wait for sale end...");
		}
		
		this.isSaled = true;
		System.out.println(Thread.currentThread().getName() + "第"+i+"部IPhone已被卖掉。");
		notify(); // 通知生产者线程该IPhone已被卖掉,可以生产下一部了。
		return this.name;
	}
}

class Producer implements Runnable {
	Iphone iphone ;
	
	public Producer(Iphone iphone) {
		this.iphone = iphone;
	}
	
	@Override
	public void run() {
		for (int i=0;i<10;i++) {
			try {
				this.iphone.setName(i+1);
				Thread.sleep(500);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
}

class Salesman implements Runnable {
	Iphone iphone;
	Salesman(Iphone iphone) {
		this.iphone = iphone;
	}
	@Override
	public void run() {
		for (int i=0;i<10;i++) {
			this.iphone.getName(i+1);
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
}

<p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">程序运行结果如下:</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-0正在生产第1部IPhone...</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-0已生产第1部IPhone...</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-1第1部IPhone已被卖掉。</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-0正在生产第2部IPhone...</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-0已生产第2部IPhone...</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-0正在生产第3部IPhone...</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-0第2部IPhone等待被卖掉。</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-1第2部IPhone已被卖掉。</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-0已生产第3部IPhone...</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-0正在生产第4部IPhone...</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-0第3部IPhone等待被卖掉。</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-1第3部IPhone已被卖掉。</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-0已生产第4部IPhone...</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-0正在生产第5部IPhone...</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-0第4部IPhone等待被卖掉。</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-1第4部IPhone已被卖掉。</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-0已生产第5部IPhone...</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-0正在生产第6部IPhone...</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-0第5部IPhone等待被卖掉。</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-1第5部IPhone已被卖掉。</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-0已生产第6部IPhone...</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-0正在生产第7部IPhone...</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-0第6部IPhone等待被卖掉。</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-1第6部IPhone已被卖掉。</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-0已生产第7部IPhone...</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-0正在生产第8部IPhone...</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-0第7部IPhone等待被卖掉。</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-1第7部IPhone已被卖掉。</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-0已生产第8部IPhone...</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-0正在生产第9部IPhone...</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-0第8部IPhone等待被卖掉。</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-1第8部IPhone已被卖掉。</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-0已生产第9部IPhone...</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-0正在生产第10部IPhone...</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-0第9部IPhone等待被卖掉。</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-1第9部IPhone已被卖掉。</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-0已生产第10部IPhone...</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-1第10部IPhone已被卖掉。</p>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值