标签:style blog http java color width
同步解决了线程中数据存取不一致的问题,而Object类中的等待与唤醒方法解决了重复存取的问题
1 package per.producterconsumer; 2 3 public class Info { 4 5 private boolean flag = true; 6 /* 7 * flag=true 表示此时可以生产,但不能消费 8 * flag=false 表示此时可以消费,但不能生产 9 */ 10 11 public synchronized void set(String name,String opterate){ 12 //同步方法,可以保证同一时刻只有一个线程在执行此方法 13 14 if(flag==false){ //表示此时可以消费,但不能生产 15 try { 16 super.wait(); //生产者等待 17 } catch (InterruptedException e) { 18 // TODO Auto-generated catch block 19 e.printStackTrace(); 20 } 21 } 22 23 this.setName(name); 24 try { 25 Thread.sleep(500); 26 } catch (InterruptedException e) { 27 // TODO Auto-generated catch block 28 e.printStackTrace(); 29 } 30 this.setOpterate(opterate); 31 32 this.flag=false; //生产完成后,将标志位改变,以便消费者来判断是否可以消费了 33 super.notify(); //唤醒消费者进程 34 } 35 36 37 public synchronized void get(){ 38 if(flag==true){ //表示此时可以生产,但不能消费 39 try { 40 super.wait(); //消费者等待 41 } catch (InterruptedException e) { 42 // TODO Auto-generated catch block 43 e.printStackTrace(); 44 } 45 } 46 try { 47 Thread.sleep(500); 48 } catch (InterruptedException e) { 49 // TODO Auto-generated catch block 50 e.printStackTrace(); 51 } 52 System.out.println(this.name+this.opterate); 53 54 this.flag=true; //消费完成后,将标志位改变,以便生产者来判断是否可以生产了 55 super.notify(); //唤醒生产者进程 56 } 57 58 59 public void setName(String name) { 60 this.name = name; 61 } 62 63 public void setOpterate(String opterate) { 64 this.opterate = opterate; 65 } 66 67 private String name="zhangsan"; 68 private String opterate="is running"; 69 70 }
<2> Producter类如下:
1 package per.producterconsumer; 2 3 public class Producter implements Runnable { 4 5 public Info info = null; 6 7 public Producter(Info info){ 8 this.info=info; 9 } 10 11 @Override 12 public synchronized void run() { 13 boolean flag = true; 14 for (int i=0;i<20;i++){ 15 if(flag){ 16 this.info.set("zhangsan"," is running"); 17 flag=false; 18 } 19 else{ 20 this.info.set("lisi"," is claming"); 21 flag=true; 22 } 23 } 24 } 25 }
<3> Consumer类如下:
1 package per.producterconsumer; 2 3 public class Consumer implements Runnable { 4 5 private Info info=null; 6 7 public Consumer(Info info){ 8 this.info=info; 9 } 10 11 @Override 12 public synchronized void run() { 13 for (int i=0;i<20;i++){ 14 this.info.get(); 15 } 16 } 17 }
<4> Test类如下:
1 package per.producterconsumer; 2 3 public class Test { 4 public static void main(String[] args) { 5 Info info = new Info(); 6 Producter p = new Producter(info); 7 Consumer c = new Consumer(info); 8 Thread t1 = new Thread(p,"t1"); 9 Thread t2 = new Thread(c,"t2"); 10 t1.start(); 11 t2.start(); 12 } 13 }
多线程中的生产者消费者Java源代码(带注释),布布扣,bubuko.com
标签:style blog http java color width
原文地址:http://www.cnblogs.com/bigdatacluster/p/3820295.html