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