本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处!

生产与消费者模式,是编程中最常用的模式之一,在多线程中应用比较明显。个人理解:在自助餐厅,厨师在不断做菜放桌子上,吃货不断从桌子上拿东西,这中间如果桌子上已经摆满那厨师要暂停工作 ,桌子上已没有食物则吃货要暂停拿东西吃。

先决条件,食材充足,桌子一定。

本程序设计原则:由于synchronized加锁方法,使得内部的所有变量也被加锁;我们采取多线程操作,故中间要用sleep(为了把锁让给别人,比如A上厕所开启sleep模式,B就偷偷拿上锁溜进来,方便后再还锁给A,要保证B修改同一个资源的话,要在A sleep的时间内完成),以便其他线程可以使用(windows多线程亦采取同样设计原则),sleep很短的时间,故用户不会有停滞感。为结果清晰,producer和consumer共执行操作50次,则程序停止运行。

桌子:

count,即桌子数量一定,produce和consume均对count操作。当蛋糕数大于等于桌子数时,生产线程要等待;当蛋糕数少于或等于0时,消费线程要等待。notifyAll是唤醒所有线程,如果生产线程watit时,消费线程已经把cake吃光,则要唤醒生产线程,反之亦然,故用notifyAll,而notify仅唤醒当前线程,在这里是没有用的。

public class Table {

	private volatile int count;
private int cake;
private int stopCounts; public Table(int count) {
// TODO Auto-generated constructor stub
this.count = count;
} public synchronized void produce(String threadName) throws InterruptedException {
while (cake >= count) {
System.out.println(threadName + " begin to wait !");
wait();
System.out.println(threadName + " stop waiting !");
}
System.out.println(threadName + " produce:" + ++cake);
notifyAll();
if (++stopCounts > 50) {
System.out.println(threadName + " stop at last !");
System.exit(0);
}
} public synchronized void consume(String threadName) throws InterruptedException {
while (cake <= 0) {
System.out.println(threadName + " begin to wait !");
wait();
System.out.println(threadName + " stop waiting !"); }
System.out.println(threadName + " consume:" + cake--);
notifyAll(); if (++stopCounts > 50) {
System.out.println(threadName + " stop at last !");
System.exit(0);
}
}
}

生产者:

生产蛋糕,一次停止1毫秒

public class Producer extends Thread {
Table table;
String threadName; public Producer(String string, Table table) {
// TODO Auto-generated constructor stub
this.table = table;
this.threadName=string;
this.setName(threadName);
} @Override
public void run() {
// TODO Auto-generated method stub
super.run();
try {
while (true) {
table.produce(threadName);
sleep(1);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

消费者:

消费蛋糕,一次停止1毫秒

public class Consumer extends Thread {
Table table;
String threadName; public Consumer(String string, Table table) {
// TODO Auto-generated constructor stub
this.table = table;
this.threadName=string;
this.setName(threadName);
} @Override
public void run() {
// TODO Auto-generated method stub
super.run();
try {
while (true) {
table.consume(threadName);
sleep(1);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

验证程序:

多线程

public class TestCP {
public static void main(String[] args) {
Table table = new Table(5);
Thread p1 = new Producer("Produce1",table);
Thread c1 = new Consumer("Consumer1",table);
Thread p2 = new Producer("Produce2",table);
Thread c2 = new Consumer("Consumer2",table);
p1.start();
c1.start();
p2.start();
c2.start();
}
}

验证结果:

Produce1 produce:1            //Producer1先生产一只cake
Produce2 produce:2 //Producer2得到锁生产一只
Consumer1 consume:2 //Consumer1先消费一只
Consumer2 consume:1 //Consumer2得到锁也消费一只
Consumer1 begin to wait ! //Consumer1得到锁开始等待
Produce1 produce:1 //Producer1开始生产
Produce2 produce:2 // Producer2开始生产
Consumer2 consume:2 //Consumer2开始消费
Consumer1 stop waiting ! //当cake数大于0,Consumer1停止等待
Consumer1 consume:1 //Consumer1开始消费
Consumer1 begin to wait ! //当cake数等于0,Consumer1开始等待
Consumer1 consume:1 //Consumer1开始消费
Consumer1 begin to wait ! //当cake数等于0,Consumer1开始等待
Produce2 produce:1 //..............................
Consumer1 stop waiting !
Consumer1 consume:1
Produce1 produce:1
Consumer2 consume:1
Consumer1 begin to wait !
Consumer2 begin to wait !
Produce1 produce:1
Produce2 produce:2
Consumer2 stop waiting !
Consumer2 consume:2
Consumer1 stop waiting !
Consumer1 consume:1
Consumer2 begin to wait !
Consumer1 begin to wait !
Produce1 produce:1
Consumer1 stop waiting !
Consumer1 consume:1
Consumer2 stop waiting !
Consumer2 begin to wait !
Produce2 produce:1
Consumer2 stop waiting !
Consumer2 consume:1
Consumer2 begin to wait !
Produce1 produce:1
Consumer1 consume:1
Consumer2 stop waiting !
Consumer2 begin to wait !
Produce2 produce:1
Consumer2 stop waiting !
Consumer2 consume:1
Produce1 produce:1
Produce2 produce:2
Consumer2 consume:2
Consumer1 consume:1
Consumer1 begin to wait !
Consumer2 begin to wait !
Produce1 produce:1
Produce2 produce:2
Consumer2 stop waiting !
Consumer2 consume:2
Consumer1 stop waiting !
Consumer1 consume:1
Consumer1 begin to wait !
Produce2 produce:1
Produce1 produce:2
Consumer1 stop waiting !
Consumer1 consume:2
Consumer2 consume:1
Consumer1 begin to wait !
Produce1 produce:1
Consumer2 consume:1
Produce2 produce:1
Consumer1 stop waiting !
Consumer1 consume:1
Consumer2 begin to wait !
Consumer1 begin to wait !
Produce1 produce:1
Consumer1 stop waiting !
Consumer1 consume:1
Consumer2 stop waiting !
Consumer2 begin to wait !
Produce2 produce:1
Consumer2 stop waiting !
Consumer2 consume:1
Consumer2 begin to wait !
Produce2 produce:1
Produce1 produce:2
Consumer1 consume:2
Consumer2 stop waiting !
Consumer2 consume:1
Consumer1 begin to wait !
Produce2 produce:1
Consumer1 stop waiting !
Consumer1 consume:1
Consumer2 begin to wait !
Produce1 produce:1
Produce1 stop at last !

需要拓展知识的请关注:

Java生产者与消费者(下)

Java生产者与消费者(上)的更多相关文章

  1. Java生产者与消费者(下)

    本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 上一讲我们让消费者和生产者都各停1毫秒,实际上大多并不是这样的.第二讲,我们讲一个极端的例子和一个正 ...

  2. java生产者与消费者模式

    前言: 生产者和消费者模式是我们在学习多线程中很经典的一个模式,它主要分为生产者和消费者,分别是两个线程, 目录 一:生产者和消费者模式简介 二:生产者和消费者模式的实现 声明:本例来源于java经典 ...

  3. java 生产者 与 消费者的案例

    主要理解了两个问题 1.线程数据同步的问题 2.线程交替运行的方式 package ThreadDemo; /** * 生产者与消费者的案例(一,同步的问题,值的问题 二,交替执行的问题) * @au ...

  4. Java 生产者模式 消费者模式

    // The standard idiom for calling the wait synchronized(sharedObject) { while(condition){ sharedObje ...

  5. Java生产者和消费者问题

    容器类Box.java public class Box { private int num = 0; public void put(){ if(num==10){ try { System.out ...

  6. java生产者,消费者

    有很多实现的方法 使用blockingqueue实现 demo import java.util.concurrent.LinkedBlockingQueue; /** * Created by 58 ...

  7. java 生产者消费者问题 并发问题的解决

    引言 生产者和消费者问题是线程模型中的经典问题:生产者和消费者在同一时间段内共用同一个存储空间,如下图所示,生产者向空间里存放数据,而消费者取用数据,如果不加以协调可能会出现以下情况: 生产者消费者图 ...

  8. java生产者消费者并发协作

    随着职务转变,代码荒废很久了,很多时间都是在沟通需求,作为一名技术员,不写代码就感觉是在自废武功,慢慢颓废了很多,今天重新回顾了下JAVA线程知识,基础知识就不梳理了,网上也很多,主要关键几个状态位( ...

  9. java 生产者消费者问题 并发问题的解决(转)

    引言 生产者和消费者问题是线程模型中的经典问题:生产者和消费者在同一时间段内共用同一个存储空间,如下图所示,生产者向空间里存放数据,而消费者取用数据,如果不加以协调可能会出现以下情况: 生产者消费者图 ...

随机推荐

  1. vue 学习笔记1 入门

    可以在 JSFiddle上在线学习vue 注意:所演示的示例,都是在JS中将Vue实例绑定至HTML中的指定元素,然后再通过Vue实例中data内的属性或者methods中的方法,来对所绑定元素的子元 ...

  2. apache(XAMPP)禁止IP访问的httpd-vhosts.conf设置

    httpd-vhosts.conf <virtualhost *:80> ServerName 123.123.123.123   ServerAlias 123.123.123.123  ...

  3. mySQL主从复制实战

    随着访问量的不断增加,单台MySQL数据库服务器压力不断增加,需要对MYSQL进行优化和架构改造,MYQSL优化如果不能明显改善压力情况,可以使用高可用.主从复制.读写分离来.拆分库.拆分表来进行优化 ...

  4. django-xadmin定制之分页显示数量

    环境:xadmin-for-python3 python3.5.2 django1.9.12 主要思路:利用django-xadmin的插件原理和原有分页插件的逻辑,单独定义一个分页显示数插件,效果如 ...

  5. Liquibase被锁

    经常运行过程中出现 Liquibase - Waiting for changelog lock Waiting for changelog lock.... Running the migratio ...

  6. Android怎样从外部跳进App

    解决问题有两个作用: 1.不用打开App直接进入某页面 2.实现App分享到外部,同一时候由外部进入App的闭环. 这个话题能够分双方面来讲.一方面是从微信进入App,还有一方面是从网页进入App. ...

  7. android内存释放处理

    不知道大家对android内存释放都做什么样的处理,本人接触android不久,近期开发小游戏的过程中,由于游戏界面组件较多.刚玩游戏的时候感觉还好,可是重复进入游戏界面玩几次之后,游戏就会卡顿,我瞬 ...

  8. 烦人的Facebook分享授权

    开发端授权app权限 facebook要求提交应用到他们平台, 并且还限制100mb, 坑爹死了, 果断使用google drive分享给他们, 最開始不确定分享给他们什么样的程序包, 结果审核没通过 ...

  9. WITH common_table_expression (Transact-SQL)

    https://docs.microsoft.com/en-us/sql/t-sql/queries/with-common-table-expression-transact-sql Specifi ...

  10. Hue的三大特点、三大功能和架构

    不多说,直接上干货! Hue 是 Cloudera 的大数据 Web 工具 官方访问网站 : http://gethue.com/ GitHub : https://github.com/cloude ...