生产者消费者模式之我的理解

我发现不管从什么话题,都能扯到Thread上面,例如:

同步-》安全-》多线程,

Core Java-》IO-》多线程,

框架-》底层-》多线程,

这样的话题导致最后的结束点,往往集中在多线程,虽然实际开发中线程级的编程,普通程序员几乎碰不到,但是还是需要我们有所了解。

找了一个生产者/消费者模式的例子,线程的很多功能的问题,都可以由它来阐述。这个例子可以用List,Stack等多种方式来模拟,这里采用最简单一个共享int,只为理解生产和消费的原理。

 

Store仓库类,为生产者和消费者所共享。对增减上锁,同步执行:

public class Store {
	private final int MAX_SIZE;
	private int count;
	public Store(int n) {
		this.MAX_SIZE = n;
		count = 0;
	}
	public synchronized void add() {
		while (count >= MAX_SIZE) {
			System.out.println("已经满了。");
			try {
				this.wait();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		count++;
		System.out.println(Thread.currentThread().toString() + " put " + count);
		this.notifyAll();
	}
	public synchronized void remove() {
		while (count <= 0) {
			System.out.println("空了 ");
		}
		try {
			this.wait();
		} catch (Exception e) {
			e.printStackTrace();
		}
		count--;
		System.out.println(Thread.currentThread().toString() + " get " + count);
		this.notify();
	}
	public static void main(String arg[]) {
		Store store = new Store(5);
		Thread pro = new Producer(store);
		Thread con = new Consumer(store);
		pro.setName("product");
		con.setName("consumer");
		Thread pro1 = new Producer(store);
		Thread con1 = new Consumer(store);
		pro1.setName("product1");
		con1.setName("consumer1");

		pro.start();
		con.start();
		pro1.start();
		con1.start();
	}
}

 

Producer生产者类。不停的add产品。每次生产后,等待1000ms,直至仓库装满。

public class Producer extends Thread {
	private Store store;
	public Producer(Store store) {
		this.store = store;
	}
	public void run() {
		while (true) {
			store.add();
			try {
				Thread.sleep(1000);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}

 

Consumer消费者类。不停的remove产品,每次减少产品后,等待1000ms,直至仓库为空。

public class Consumer extends Thread {
	private Store store;
	public Consumer(Store store) {
		this.store = store;
	}
	public void run() {
		while (true) {
			store.remove();
			try {
				Thread.sleep(1000);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}

 

 贴上运行的结果:

Thread[product,5,main] put 1
Thread[product1,5,main] put 2
Thread[consumer,5,main] get 1
Thread[product1,5,main] put 2
Thread[consumer1,5,main] get 1
Thread[product,5,main] put 2
Thread[consumer,5,main] get 1
Thread[product1,5,main] put 2
Thread[product,5,main] put 3
Thread[consumer,5,main] get 2
Thread[consumer1,5,main] get 1
Thread[product1,5,main] put 2
Thread[product,5,main] put 3
Thread[product1,5,main] put 4
Thread[consumer1,5,main] get 3
Thread[consumer,5,main] get 2
Thread[product,5,main] put 3
Thread[product1,5,main] put 4
Thread[consumer1,5,main] get 3
Thread[consumer,5,main] get 2
Thread[product,5,main] put 3
Thread[product1,5,main] put 4
Thread[consumer1,5,main] get 3
Thread[consumer,5,main] get 2
Thread[product,5,main] put 3
Thread[product1,5,main] put 4
Thread[consumer1,5,main] get 3
Thread[consumer,5,main] get 2
Thread[product,5,main] put 3
Thread[product1,5,main] put 4
Thread[consumer1,5,main] get 3
Thread[consumer,5,main] get 2
Thread[product,5,main] put 3
Thread[product,5,main] put 4
Thread[consumer1,5,main] get 3
Thread[consumer,5,main] get 2
Thread[product1,5,main] put 3
Thread[product,5,main] put 4
Thread[consumer1,5,main] get 3
Thread[consumer,5,main] get 2
Thread[product1,5,main] put 3
Thread[product,5,main] put 4
Thread[consumer1,5,main] get 3
Thread[consumer,5,main] get 2
Thread[product1,5,main] put 3
Thread[product1,5,main] put 4
Thread[product,5,main] put 5
Thread[consumer1,5,main] get 4
Thread[consumer,5,main] get 3
Thread[product1,5,main] put 4
Thread[product,5,main] put 5
Thread[consumer,5,main] get 4
Thread[consumer1,5,main] get 3
Thread[product,5,main] put 4
Thread[product1,5,main] put 5
Thread[consumer1,5,main] get 4
Thread[consumer,5,main] get 3
Thread[product,5,main] put 4
Thread[product1,5,main] put 5
Thread[consumer1,5,main] get 4
Thread[consumer,5,main] get 3
Thread[product1,5,main] put 4
Thread[product,5,main] put 5
已经满了。
已经满了。

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值