package com.gh.thread;
/**
* 生产者和消费者案例
* wait和sleep的区别
* wait不让出监视器锁,sleep让出监视器的锁
* @author ganhang
*
*/
/**
* 生产者
* @author ganhang
*
*/
class Producter implements Runnable{
private Food food;
public Producter(Food food){
this.food=food;
}
@Override
public void run() {
for(int i=0;i<100;i++){
if(i%2==0){
food.product("韭菜", "韭菜有益");
}else{
food.product("腰花", "腰花好吃");
}
}
} }
/**
* 消费者
* @author ganhang
*
*/
class Consumer implements Runnable{
private Food food;
public Consumer(Food food) {
this.food=food;
}
@Override
public void run() {
for(int i=0;i<100;i++){
food.get();
}
} }
/**
* 产品对象
* @author ganhang
*
*/
class Food{
private String name;
private String info;
private boolean flag=true;//true表示可以生产,false表示可以消费
//生产产品
public synchronized void product(String name,String info){
if(!flag){//如果为false说明正在消费
try {
//让当前线程进入等待池等待,没有设置时间,需要其他线程唤醒,释放对象锁,让出cpu;
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.setName(name);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.setInfo(info);
System.out.println(Thread.currentThread().getName()+"--生产出--"+
this.getName()+"-"+this.getInfo());
flag=false;//生产完成表示可以进行消费了
this.notify();//唤醒该监视器的一个线程
}
//消费产品
public synchronized void get(){
if(flag){//如果为ture说明正在生产
try {
this.wait();//同样让它等待
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"--已送出--"+
this.getName()+"-"+this.getInfo());
flag=true;//消费完可以生产了
this.notify();//唤醒一个线程
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getInfo() {
return info;
}
public Food() {
super();
}
public Food(String name, String info) {
super();
this.name = name;
this.info = info;
}
@Override
public String toString() {
return "food [name=" + name + ", info=" + info + "]";
}
public void setInfo(String info) {
this.info = info;
}
}
public class ThreadDemo {
public static void main(String[] args) {
Food f=new Food();
Producter p=new Producter(f);
Consumer c=new Consumer(f);
new Thread(p,"线程p").start();
new Thread(c,"线程c").start();
}
}

输出

线程p--生产出--韭菜-韭菜有益
线程c--已送出--韭菜-韭菜有益
线程p--生产出--腰花-腰花好吃
线程c--已送出--腰花-腰花好吃
线程p--生产出--韭菜-韭菜有益
线程c--已送出--韭菜-韭菜有益
线程p--生产出--腰花-腰花好吃
线程c--已送出--腰花-腰花好吃
线程p--生产出--韭菜-韭菜有益
线程c--已送出--韭菜-韭菜有益
线程p--生产出--腰花-腰花好吃
线程c--已送出--腰花-腰花好吃
线程p--生产出--韭菜-韭菜有益
线程c--已送出--韭菜-韭菜有益
线程p--生产出--腰花-腰花好吃
线程c--已送出--腰花-腰花好吃
线程p--生产出--韭菜-韭菜有益

。。。。

最新文章

  1. JavaScript操作DOM对象
  2. Latency
  3. SendTextMessage如何打开记事本并显示指定内容
  4. ZOJ-3686 A Simple Tree Problem 线段树
  5. MySQL增加列,移动列
  6. js基础第八天
  7. linux中转换编码
  8. #include &lt;limits.h&gt;
  9. 转:【Java并发编程】之十九:并发新特性—Executor框架与线程池(含代码)
  10. SSH网上商城---邮件发送
  11. iOS中判断照片和相机权限
  12. 错误:无效参数:could not find capabilities for arch=aarch64
  13. C# wave mp3 播放器探寻
  14. 在Ubuntu 14.04 LTS系统中设置Apache虚拟主机(一IP多访问)
  15. build-your-own-lisp
  16. Android学习笔记——Intents 和 Intent Filters(二)
  17. mysql 错误:1166 解决办法
  18. 三、微信小游戏开发 --- 小游戏API调用Platform
  19. mysql底层实现
  20. oracle查询数据库最大连接数等信息

热门文章

  1. xcode Simulated Metrics xib设置小问题
  2. ShineTime 是一个效果非常精致的缩略图相册
  3. spring AOP简单入门
  4. 笔记 postgresql oid同步
  5. Problem B The Blocks Problem(vector的使用)
  6. 删除Lb重复的数,用La输出(顺序表)
  7. windows如何安装scrapy
  8. IOS 播放音频
  9. QString类的使用(无所不包,极其方便)
  10. QT实现拖放文件(有例子,并且图文并茂,非常清楚)