zoukankan      html  css  js  c++  java
  • 线程的通信-生产者消费者

    当对于一个生产者和一个消费者时

    public static void main(String[] agrs) {
    	Resource res = new Resource();
    	Producer p = new Producer(res);
    	Consumer c = new Consumer(res);
    	
    	Thread t1 = new Thread(p);
    	Thread t2 = new Thread(c);
    	t1.start();
    	t2.start();
    }
    
    class Resource {
    	private String name;
    	private int cont = 1;
    	private boolean flag = false;
    	public synchronized void set(String name) {
    		if(flag) {
    			try{
    				this.wait();
    			} catch (InterruptedException e) {
    				e.printStackTrace();
    			}
    		}
    		this.name = name + "--" + cont++;
    		System.out.println(Thread.currentThread().getName()+"- producer -"+this.name);
    		flag = true;
    		this.notify();
    	}
    	public synchronized void out() {
    		if(!flag) {
    			try {
    				this.wait();
    			} catch (InterruptedException e) {
    				e.printStackTrace();
    			}
    		}
    		System.out.println(Thread.currentThread().getName()+"---- consumer ----"+this.name);
    		flag = false;
    		this.notify();
    	}
    }
    
    class Producer implements Runnable {
    	private Resource res;
    	Producer(Resource res) {
    		this.res = res;
    	}
    	public void run() {
    		while(true) {
    			res.set("wares");
    		}
    	}
    }
    
    class Consumer implements Runnable {
    	private Resource res;
    	Consumer(Resource res) {
    		this.res = res;
    	}
    	public void run() {
    		while(true) {
    			res.out();
    		}
    	}
    } 
    

    当对于多个生产者和消费者时###

    public static void main(String[] agrs) {
    		Resource res = new Resource();
    		Producer p = new Producer(res);
    		Consumer c = new Consumer(res);
    		
    		Thread t1 = new Thread(p);
    		Thread t2 = new Thread(p);
    		Thread t3 = new Thread(c);
    		Thread t4 = new Thread(c);
    		
    		t1.start();
    		t2.start();
    		t3.start();
    		t4.start();
    	}
    

    会出现生产一个商品,消费出两个商品,或者生产两个而消费一个

    这是因为if语句只判断一次flag标记,消费者的线程生产完会唤醒本方的另一条生产线程,而不判断flag标记,导致生产两次而消费一次
    

    然后我们可以把if换成while
    原因:让被唤醒的线程再一次的判断

    while(flag) {
    	try{
    		this.wait();
    	} catch (InterruptedException e) {
    		e.printStackTrace();
    	}
    }
    

    但是,新的问题又产生了,线程会全部停止
    原因:线程唤醒往往是唤醒线程池的第一个,所以会唤醒本方的线程,会导致数据错乱,加入while后,会导致全部的线程都在等待。我们应该唤醒的是消费者的线程,所以我们可以用notifyAll,唤醒所有的线程,因为会while会判断flag标记,所以不会唤醒生产者的线程,而是唤醒消费者的线程
    notify,容易出现只唤醒本方线程的情况,导致程序中的所有线程等待

    while(flag) {
    	try{
    		this.wait();
    	} catch (InterruptedException e) {
    		e.printStackTrace();
    	}
    }
    this.name = name + "--" + cont++;
    System.out.println(Thread.currentThread().getName()+"- producer -"+this.name);
    flag = true;
    this.notifyALL();
    

    最后改进

    public class Dealer {
    	public static void main(String[] agrs) {
    		Resource res = new Resource();
    		Producer p = new Producer(res);
    		Consumer c = new Consumer(res);
    		
    		Thread t1 = new Thread(p);
    		Thread t2 = new Thread(p);
    		Thread t3 = new Thread(c);
    		Thread t4 = new Thread(c);
    		
    		t1.start();
    		t2.start();
    		t3.start();
    		t4.start();
    	}
    }
    
    class Resource {
    	private String name;
    	private int cont = 1;
    	private boolean flag = false;
    	public synchronized void set(String name) {
    		while(flag) {
    			try{
    				this.wait();
    			} catch (InterruptedException e) {
    				e.printStackTrace();
    			}
    		}
    		this.name = name + "--" + cont++;
    		System.out.println(Thread.currentThread().getName()+"- producer -"+this.name);
    		flag = true;
    		this.notifyALL();
    	}
    	public synchronized void out() {
    		while(!flag) {
    			try {
    				this.wait();
    			} catch (InterruptedException e) {
    				e.printStackTrace();
    			}
    		}
    		System.out.println(Thread.currentThread().getName()+"---- consumer ----"+this.name);
    		flag = false;
    		this.notifyAll();
    	}
    }
    
    class Producer implements Runnable {
    	private Resource res;
    	Producer(Resource res) {
    		this.res = res;
    	}
    	public void run() {
    		while(true) {
    			res.set("wares");
    		}
    	}
    }
    
    class Consumer implements Runnable {
    	private Resource res;
    	Consumer(Resource res) {
    		this.res = res;
    	}
    	public void run() {
    		while(true) {
    			res.out();
    		}
    	}
    } 
    
  • 相关阅读:
    第24章、OnLongClickListener长按事件(从零开始学Android)
    第21章、OnItemSelectedListener事件(从零开始学Android)
    GIS开源软件大全
    How to configure SRTM elevations in WorldWind WMS
    [转]SRTM、ASTER GDEM等全球数字高程数据(DEM)下载方式简介
    SRTM数据介绍与说明
    [转]基于四叉树(QuadTree)的LOD地形实现
    [转]World Wind学习总结一
    [转]World Wind Java开发之四——搭建本地WMS服务器
    [转]World Wind Java开发之五——读取本地shp文件
  • 原文地址:https://www.cnblogs.com/rancvl/p/5466465.html
Copyright © 2011-2022 走看看