标签:开始 生产者和消费者 target 需要 art 代码 机制 strong log
转载请注明出处:http://blog.csdn.net/ns_code/article/details/17249321
生产者消费者问题是线程模型中的经典问题:生产者和消费者在同一时间段内共用同一存储空间,生产者向空间里生产数据,而消费者取走数据。
这里实现如下情况的生产--消费模型:
生产者不断交替地生产两组数据“姓名--1 --> 内容--1”,“姓名--2--> 内容--2”,消费者不断交替地取得这两组数据,这里的“姓名--1”和“姓名--2”模拟为数据的名称,“内容--1 ”和“内容--2 ”模拟为数据的内容。
由于本程序中牵扯到线程运行的不确定性,因此可能会出现以下问题:
1、假设生产者线程刚向数据存储空间添加了数据的名称,还没有加入该信息的内容,程序就切换到了消费者线程,消费者线程将把信息的名称和上一个信息的内容联系在一起;
2、生产者生产了若干次数据,消费者才开始取数据,或者是,消费者取完一次数据后,还没等生产者放入新的数据,又重复取出了已取过的数据。
问题1很明显要靠同步来解决,问题2则需要线程间通信,生产者线程放入数据后,通知消费者线程取出数据,消费者线程取出数据后,通知生产者线程生产数据,这里用wait/notify机制来实现。
详细的实现代码如下:
- class Info{
- private String name = "name";
- private String content = "content" ;
- private boolean flag = true ;
- public synchronized void set(String name,String content){
- while(!flag){
- try{
- super.wait() ;
- }catch(InterruptedException e){
- e.printStackTrace() ;
- }
- }
- this.setName(name) ;
- try{
- Thread.sleep(300) ;
- }catch(InterruptedException e){
- e.printStackTrace() ;
- }
- this.setContent(content) ;
- flag = false ;
- super.notify();
- }
- public synchronized void get(){
- while(flag){
- try{
- super.wait() ;
- }catch(InterruptedException e){
- e.printStackTrace() ;
- }
- }
- try{
- Thread.sleep(300) ;
- }catch(InterruptedException e){
- e.printStackTrace() ;
- }
- System.out.println(this.getName() +
- " --> " + this.getContent()) ;
- flag = true ;
- super.notify();
- }
- public void setName(String name){
- this.name = name ;
- }
- public void setContent(String content){
- this.content = content ;
- }
- public String getName(){
- return this.name ;
- }
- public String getContent(){
- return this.content ;
- }
- }
- class Producer implements Runnable{
- private Info info = null ;
- public Producer(Info info){
- this.info = info ;
- }
- public void run(){
- boolean flag = true ;
- for(int i=0;i<10;i++){
- if(flag){
- this.info.set("姓名--1","内容--1") ;
- flag = false ;
- }else{
- this.info.set("姓名--2","内容--2") ;
- flag = true ;
- }
- }
- }
- }
- class Consumer implements Runnable{
- private Info info = null ;
- public Consumer(Info info){
- this.info = info ;
- }
- public void run(){
- for(int i=0;i<10;i++){
- this.info.get() ;
- }
- }
- }
- public class ThreadCaseDemo03{
- public static void main(String args[]){
- Info info = new Info();
- Producer pro = new Producer(info) ;
- Consumer con = new Consumer(info) ;
- new Thread(pro).start() ;
-
- try{
- Thread.sleep(500) ;
- }catch(InterruptedException e){
- e.printStackTrace() ;
- }
-
- new Thread(con).start() ;
- }
- }
执行结果如下:

另外,在run方法中,二者循环的次数要相同,否则,当一方的循环结束时,另一方的循环依然继续,它会阻塞在wait()方法处,而等不到对方的notify通知。
转: 【Java并发编程】之十三:生产者—消费者模型(含代码)
标签:开始 生产者和消费者 target 需要 art 代码 机制 strong log
原文地址:http://www.cnblogs.com/xuyatao/p/6919461.html