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

来源:互联网 发布:单片机433接收程序 编辑:程序博客网 时间:2024/05/16 17:03

通过信号量,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 blocke.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;}@Overridepublic 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 blocke.printStackTrace();}}}}class Salesman implements Runnable {Iphone iphone;Salesman(Iphone iphone) {this.iphone = iphone;}@Overridepublic 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>

0 0
原创粉丝点击