java并发控制经典场景-生产者/消费者_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > java并发控制经典场景-生产者/消费者

java并发控制经典场景-生产者/消费者

 2012/3/6 13:51:25  lhc1986  程序员俱乐部  我要评论(0)
  • 摘要:java并发控制四种方法:1.wait()/notify();2.await()/notify();3.BlockingQueue阻塞队列方法;4.PipedInputStream/PipedOutputStream最常用的是wait()/notify(),简单demo见代码:仓库类:importjava.util.LinkedList;publicclassStorage{publicstaticfinalintMAX_SIZE=100;privateLinkedList<
  • 标签:经典 Java Java并发 消费者

java并发控制四种方法

1.wait()/notify();

2.await()/notify();

3.BlockingQueue 阻塞队列方法;

4.PipedInputStream / PipedOutputStream


最常用的是wait()/notify(),简单demo见代码:

?

?

仓库类:

import java.util.LinkedList;

public class Storage {

	public static final int MAX_SIZE = 100;

	private LinkedList<Object> list = new LinkedList<Object>();

	public void produce(int num) {
		synchronized (list) {
			while (num + list.size() > Storage.MAX_SIZE) {
				System.out.println("要生产的产品数量:" + num + "库存量"
						+ list.size() + "暂不能执行生产任务");
				try {
					list.wait();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			for (int i = 0; i < num; i++) {
				list.add(new Object());
			}
			System.out.println("已经生产产品数:" + num + "现库存量为:" + list.size());
			list.notifyAll();
		}
	}

	public void consume(int num) {
		synchronized (list) {
			while (num > list.size()) {
				System.out.println("要消费的产品数量:" + num + "库存量:"
						+ list.size() + "暂时不能执行消费任务!");
				try {
					list.wait();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			for (int i = 1; i <= num; ++i) {
				list.remove();
			}

			System.out.println("已经消费产品数:" + num + "现库存量为:" + list.size());

			list.notifyAll();
		}
	}

	public LinkedList<Object> getList() {
		return list;
	}

	public void setList(LinkedList<Object> list) {
		this.list = list;
	}
	
}

?生产者类:

public class Producer implements Runnable {
	private int num;
	
	private Storage storage;
	
	public Producer(Storage storage){
		this.storage = storage;
	}

	@Override
	public void run() {
		this.produce();
	}
	
	public void produce(){
		this.storage.produce(this.num);
	}

	public int getNum() {
		return num;
	}

	public void setNum(int num) {
		this.num = num;
	}

	public Storage getStorage() {
		return storage;
	}

	public void setStorage(Storage storage) {
		this.storage = storage;
	}

}

?消费者类:

public class Consumer implements Runnable {
	
	private int num;
	
	private Storage storage;
	
	public Consumer(Storage storage){
		this.storage = storage;
	}

	@Override
	public void run() {
		this.consume();
	}
	
	private void consume(){
		this.storage.consume(this.num);
	}

	public int getNum() {
		return num;
	}

	public void setNum(int num) {
		this.num = num;
	}

	public Storage getStorage() {
		return storage;
	}

	public void setStorage(Storage storage) {
		this.storage = storage;
	}
	
	
	
}

?测试方法类:

public class SysTest {
	public static void main(String[] args) {
		Storage storage = new Storage();
		Producer pt1 = new Producer(storage);
		pt1.setNum(10);
		Producer pt2 = new Producer(storage);
		pt2.setNum(10);
		Producer pt3 = new Producer(storage);
		pt3.setNum(10);
		Producer pt4 = new Producer(storage);
		pt4.setNum(10);
		Producer pt5 = new Producer(storage);
		pt5.setNum(10);
		Producer pt6 = new Producer(storage);
		pt6.setNum(10);
		Producer pt7 = new Producer(storage);
		pt7.setNum(80);
		
		
		Thread p1 = new Thread(pt1);
		Thread p2 = new Thread(pt2);
		Thread p3 = new Thread(pt3);
		Thread p4 = new Thread(pt4);
		Thread p5 = new Thread(pt5);
		Thread p6 = new Thread(pt6);
		Thread p7 = new Thread(pt7);
		
		
		Consumer ct1 = new Consumer(storage);
		ct1.setNum(50);
		Consumer ct2 = new Consumer(storage);
		ct2.setNum(20);
		Consumer ct3 = new Consumer(storage);
		ct3.setNum(30);
		
		Thread c1 = new Thread(ct1);
		Thread c2 = new Thread(ct2);
		Thread c3 = new Thread(ct3);
		
		
		c1.start();
		c2.start();
		c3.start();
		
		p1.start();
		p2.start();
		p3.start();
		p4.start();
		p5.start();
		p6.start();
		p7.start();
		
	}
}

?结果:

要消费的产品数量:50库存量:0暂时不能执行消费任务!
要消费的产品数量:30库存量:0暂时不能执行消费任务!
已经生产产品数:10现库存量为:10
要消费的产品数量:30库存量:10暂时不能执行消费任务!
要消费的产品数量:20库存量:10暂时不能执行消费任务!
要消费的产品数量:50库存量:10暂时不能执行消费任务!
已经生产产品数:10现库存量为:20
已经生产产品数:10现库存量为:30
已经生产产品数:10现库存量为:40
要消费的产品数量:50库存量:40暂时不能执行消费任务!
已经消费产品数:20现库存量为:20
要消费的产品数量:30库存量:20暂时不能执行消费任务!
已经生产产品数:10现库存量为:30
要消费的产品数量:50库存量:30暂时不能执行消费任务!
要生产的产品数量:80库存量30暂不能执行生产任务
已经消费产品数:30现库存量为:0
已经生产产品数:10现库存量为:10
已经生产产品数:80现库存量为:90
已经消费产品数:50现库存量为:40

发表评论
用户名: 匿名