博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java多线程的生产者、消费者实现例子
阅读量:5827 次
发布时间:2019-06-18

本文共 3747 字,大约阅读时间需要 12 分钟。

hot3.png

/** * 要生产的产品 * @author jimmywu * */public class Product {    private int id;    private String name;        public Product(int id,String name){    	this.id = id;    	this.name = name;    }    public int getId() {		return id;	}	public void setId(int id) {		this.id = id;	}	public String getName() {		return name;	}	public void setName(String name) {		this.name = name;	}	public String toString(){		return "Product id:"+id+" name:"+name;    	    }}/** * 仓库类 *  * @author jimmywu * */public class Store {	Product[] pArray = new Product[10];	AtomicInteger index = new AtomicInteger(0);	/**	 * 生产方法	 * 	 * @param p	 */	public synchronized void push(Product p) {		try {			while (index.get() == pArray.length) {				System.out.println("仓库已满!!!!!");				this.wait();			}			this.notify();		} catch (InterruptedException e) {			// TODO Auto-generated catch block			e.printStackTrace();		}		pArray[index.getAndIncrement()] = p;		System.out.println(Thread.currentThread().getName() + " 当前生产:"				+ p.toString() + " 共" + index.get() + "个产品");	}	/**	 * 消费方法	 * 	 * @return	 */	public synchronized Product pop() {		try {			while (index.get() == 0) {				System.out.println("仓库空啦!!!!");				this.wait();			}			this.notify();		} catch (InterruptedException e) {			e.printStackTrace();		}		Product p = pArray[index.getAndDecrement() - 1];		System.out.println(Thread.currentThread().getName() + " 消费了"				+ p.toString() + "当前还有" + index.get() + "个");		return p;	}	public static void main(String[] args) {		Store s = new Store();		Thread p1 = new Thread(new Producer(s));		Thread p2 = new Thread(new Producer(s));		Thread c1 = new Thread(new Consumer(s));		Thread c2 = new Thread(new Consumer(s));		p1.start();//		 p2.start();		c1.start();		 c2.start();	}}class Producer implements Runnable {	Store store;	Producer(Store store) {		this.store = store;	}	/**	 *  生产线程.	 */	public void run() {//		for (int i = 0; i < 20; i++) {		while(true){			Product p = new Product(new Random().nextInt(), String.valueOf(System					.currentTimeMillis()));			store.push(p);			try {				Thread.sleep((int) (Math.random() * 500));			} catch (InterruptedException e) {				e.printStackTrace();			}		}		//		}	}}class Consumer implements Runnable {	Store store;	Consumer(Store store) {		this.store = store;	}	/**	 *  消费线程.	 */	public void run() {//		for (int i = 0; i < 20; i++) {		while(true){			Product p = store.pop();					try {				Thread.sleep((int) (Math.random() * 1000));			} catch (InterruptedException e) {				e.printStackTrace();			}		}		//		}	}}

执行输出:

仓库空啦!!!!

仓库空啦!!!!
Thread-0 当前生产:Product id:-1301795692 name:1483024321535 共1个产品
Thread-2 消费了Product id:-1301795692 name:1483024321535当前还有0个
仓库空啦!!!!
Thread-0 当前生产:Product id:-527262198 name:1483024321695 共1个产品
Thread-3 消费了Product id:-527262198 name:1483024321695当前还有0个
Thread-0 当前生产:Product id:-562347048 name:1483024321848 共1个产品
Thread-0 当前生产:Product id:1334829650 name:1483024322076 共2个产品
Thread-3 消费了Product id:1334829650 name:1483024322076当前还有1个
Thread-0 当前生产:Product id:503010454 name:1483024322213 共2个产品
Thread-2 消费了Product id:503010454 name:1483024322213当前还有1个
Thread-2 消费了Product id:-562347048 name:1483024321848当前还有0个
仓库空啦!!!!
Thread-0 当前生产:Product id:1339084264 name:1483024322545 共1个产品
Thread-3 消费了Product id:1339084264 name:1483024322545当前还有0个
Thread-0 当前生产:Product id:-704459171 name:1483024322562 共1个产品
Thread-2 消费了Product id:-704459171 name:1483024322562当前还有0个
Thread-0 当前生产:Product id:1375661799 name:1483024323006 共1个产品
Thread-0 当前生产:Product id:-584270083 name:1483024323087 共2个产品
Thread-3 消费了Product id:-584270083 name:1483024323087当前还有1个
Thread-3 消费了Product id:1375661799 name:1483024323006当前还有0个
仓库空啦!!!!
Thread-0 当前生产:Product id:777716577 name:1483024323489 共1个产品

转载于:https://my.oschina.net/u/2263956/blog/815803

你可能感兴趣的文章
linux svn安装和配置
查看>>
SSH中调用另一action的方法(chain,redirect)
查看>>
数据库基础
查看>>
表格排序
查看>>
关于Android四大组件的学习总结
查看>>
java只能的round,ceil,floor方法的使用
查看>>
由于无法创建应用程序域,因此未能执行请求。错误: 0x80070002 系统找不到指定的文件...
查看>>
新开的博客,为自己祝贺一下
查看>>
【CQOI2011】放棋子
查看>>
采用JXL包进行EXCEL数据写入操作
查看>>
一周总结
查看>>
将txt文件转化为json进行操作
查看>>
线性表4 - 数据结构和算法09
查看>>
Online Patching--EBS R12.2最大的改进
查看>>
Binary Search Tree Iterator leetcode
查看>>
uva-317-找规律
查看>>
Event事件的兼容性(转)
查看>>
我的2014-相对奢侈的生活
查看>>
zoj 2412 dfs 求连通分量的个数
查看>>
Java设计模式
查看>>