java多线程同步生产者消费者问题
Posted xiongjiawei
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java多线程同步生产者消费者问题相关的知识,希望对你有一定的参考价值。
1 package 多线程; 2 class Producer implements Runnable{ 3 private Data data; 4 public Producer(Data data){ 5 this.data=data; 6 } 7 @Override 8 public synchronized void run() { 9 for(int i=0;i<50;i++){ 10 if(i%2==0){ 11 this.data.setTitle("饼干"); 12 try { 13 Thread.sleep(100); 14 } catch (InterruptedException e) { 15 e.printStackTrace(); 16 } 17 this.data.setValue("麦香饼干"); 18 }else{ 19 this.data.setTitle("饮料"); 20 try { 21 Thread.sleep(100); 22 } catch (InterruptedException e) { 23 e.printStackTrace(); 24 } 25 this.data.setValue("果汁好喝"); 26 } 27 } 28 } 29 } 30 class Consumer implements Runnable{ 31 private Data data; 32 public Consumer(Data data){ 33 this.data=data; 34 } 35 @Override 36 public synchronized void run() { 37 for(int i=0;i<50;i++){ 38 try { 39 Thread.sleep(100); 40 } catch (InterruptedException e) { 41 e.printStackTrace(); 42 } 43 System.out.println("消费:"+this.data.getTitle()+"->"+this.data.getValue()); 44 } 45 } 46 } 47 class Data{ 48 private String title; 49 private String value; 50 public void setTitle(String title) { 51 this.title = title; 52 } 53 public String getTitle() { 54 return title; 55 } 56 public void setValue(String value) { 57 this.value = value; 58 } 59 public String getValue() { 60 return value; 61 } 62 } 63 public class TestProducerConsumer { 64 public static void main(String[] args) { 65 Data data=new Data(); 66 new Thread(new Producer(data)).start(); 67 new Thread(new Consumer(data)).start(); 68 } 69 }
1 package 多线程; 2 class Producer implements Runnable{ 3 private Data data; 4 public Producer(Data data){ 5 this.data=data; 6 } 7 @Override 8 public void run() { 9 for(int i=0;i<50;i++){ 10 if(i%2==0){ 11 this.data.set("饼干","麦香饼干"); 12 }else{ 13 this.data.set("饮料","果汁好喝"); 14 } 15 } 16 } 17 } 18 class Consumer implements Runnable{ 19 private Data data; 20 public Consumer(Data data){ 21 this.data=data; 22 } 23 @Override 24 public void run() { 25 for(int i=0;i<50;i++){ 26 System.out.println("消费:"+this.data.get()); 27 } 28 } 29 } 30 class Data{ 31 private String title; 32 private String value; 33 public synchronized void set(String title,String value) { 34 //若不加synchronized仍会有数据错乱现象 35 this.title = title; 36 try { 37 Thread.sleep(100); 38 } catch (InterruptedException e) { 39 e.printStackTrace(); 40 } 41 this.value=value; 42 } 43 public synchronized String get() { 44 try { 45 Thread.sleep(100); 46 } catch (InterruptedException e) { 47 e.printStackTrace(); 48 } 49 return title+"->"+value; 50 } 51 } 52 public class TestProducerConsumer { 53 public static void main(String[] args) { 54 Data data=new Data(); 55 new Thread(new Producer(data)).start(); 56 new Thread(new Consumer(data)).start(); 57 } 58 }
1 package 多线程; 2 class Producer implements Runnable{ 3 private Data data; 4 public Producer(Data data){ 5 this.data=data; 6 } 7 @Override 8 public void run() { 9 for(int i=0;i<50;i++){ 10 if(i%2==0){ 11 this.data.set("饼干","麦香饼干"); 12 }else{ 13 this.data.set("饮料","果汁好喝"); 14 } 15 } 16 } 17 } 18 class Consumer implements Runnable{ 19 private Data data; 20 public Consumer(Data data){ 21 this.data=data; 22 } 23 @Override 24 public void run() { 25 for(int i=0;i<50;i++){ 26 System.out.println("消费:"+this.data.get()); 27 } 28 } 29 } 30 class Data{ 31 private boolean flag=true;//如果不初始设置为true可能会在第一个消费时取得null 32 private String title; 33 private String value; 34 public synchronized void set(String title,String value) { 35 if(this.flag==false) 36 try { 37 super.wait(); 38 } catch (InterruptedException e1) { 39 e1.printStackTrace(); 40 } 41 //若不加synchronized仍会有数据错乱现象 42 this.title = title; 43 try { 44 Thread.sleep(100); 45 } catch (InterruptedException e) { 46 e.printStackTrace(); 47 } 48 this.value=value; 49 this.flag=false; 50 super.notify(); 51 } 52 public synchronized String get() { 53 if(this.flag==true) 54 try { 55 super.wait(); 56 } catch (InterruptedException e1) { 57 e1.printStackTrace(); 58 } 59 try { 60 Thread.sleep(100); 61 } catch (InterruptedException e) { 62 e.printStackTrace(); 63 } 64 this.flag=true; 65 super.notify(); 66 return title+"->"+value; 67 } 68 } 69 public class TestProducerConsumer { 70 public static void main(String[] args) { 71 Data data=new Data(); 72 new Thread(new Producer(data)).start(); 73 new Thread(new Consumer(data)).start(); 74 } 75 }
以上是关于java多线程同步生产者消费者问题的主要内容,如果未能解决你的问题,请参考以下文章
Java多线程-同步:synchronized 和线程通信:生产者消费者模式