首页 技术 正文
技术 2022年11月15日
0 收藏 776 点赞 4,577 浏览 2528 个字

/*
* 生产者和消费者案例
*/
public class TestProductorAndConsumer { public static void main(String[] args) {
Clerk clerk = new Clerk(); Productor pro = new Productor(clerk);
Consumer cus = new Consumer(clerk); new Thread(pro, "生产者 A").start();
new Thread(cus, "消费者 B").start();
}}//店员
class Clerk{
private int product = 0; //进货
public synchronized void get(){//循环次数:0
while(product >= 1){//为了避免虚假唤醒问题,应该总是使用在循环中
System.out.println("产品已满!"); try {
this.wait();
} catch (InterruptedException e) {
} } System.out.println(Thread.currentThread().getName() + " : " + ++product);
this.notifyAll();
} //卖货
public synchronized void sale(){//product = 0; 循环次数:0
while(product <= 0){
System.out.println("缺货!"); try {
this.wait();
} catch (InterruptedException e) {
}
} System.out.println(Thread.currentThread().getName() + " : " + --product);
this.notifyAll();
}
}//生产者
class Productor implements Runnable{
private Clerk clerk; public Productor(Clerk clerk) {
this.clerk = clerk;
} @Override
public void run() {
for (int i = 0; i < 20; i++) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
} clerk.get();
}
}
}//消费者
class Consumer implements Runnable{
private Clerk clerk; public Consumer(Clerk clerk) {
this.clerk = clerk;
} @Override
public void run() {
for (int i = 0; i < 20; i++) {
clerk.sale();
}
}
}
 

上边代码主要介绍了java多线程解决生产者消费者问题的方法,实例分析了java采用多线程的方法解决生产者消费者问题的相关技巧,需要的朋友可以参考下

另外concurrent 包下面在执行多线程的时候也给出了特性阻塞队列 BlockingQueue   用法如下:也可以实现生产者和消费者模式

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;public class Test {
public static void main(String[] args) {
//固定容器大小为10
BlockingQueue<Food> foods = new LinkedBlockingQueue<Food>(10);
Thread produce = new Thread(new Produce(foods));
Thread consume = new Thread(new Consume(foods));
produce.start();
consume.start();
}
}/**
* 生产者
*/
class Produce implements Runnable {
private BlockingQueue<Food> foods; Produce(BlockingQueue<Food> foods) {
this.foods = foods;
} @Override
public void run() {
int i = 0;
while (i <= 10) {
try {
//当生产的食品数量装满了容器,那么在while里面该食品容器(阻塞队列)会自动阻塞 wait状态 等待消费
foods.put(new Food("食品" + i));
i++;
} catch (InterruptedException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
}
}/**
* 消费者
*/
class Consume implements Runnable {
private BlockingQueue<Food> foods; Consume(BlockingQueue<Food> foods) {
this.foods = foods;
} @Override
public void run() {
try {
Thread.sleep(1000); //用于测试当生产者生产满10个食品后是否进入等待状态
while (true) {
//当容器里面的食品数量为空时,那么在while里面该食品容器(阻塞队列)会自动阻塞 wait状态 等待生产
Food food = foods.take();
System.out.println("消费" + food.getName());
}
} catch (InterruptedException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
}/**
* 食品
*/
class Food {
private String name; String getName() {
return name;
} Food(String name) {
this.name = name;
System.out.println("生产" + name);
}
}

  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,997
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,511
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,356
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,139
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,770
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,848