java多线程生产者消费者问题_java多线程解决生产者消费者问题

上述代码展示了Java中通过多线程解决生产者消费者问题的实例,利用`Clerk`类实现同步控制,以及通过`BlockingQueue`实现线程间的通信。生产者`Productor`类将产品放入队列,消费者`Consumer`类从队列中取出产品。此外,还展示了如何使用`LinkedBlockingQueue`作为阻塞队列来简化生产者消费者模式的实现。
摘要由CSDN通过智能技术生成

/*

* 生产者和消费者案例

*/

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 foods = new LinkedBlockingQueue(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 foods;

Produce(BlockingQueue 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 foods;

Consume(BlockingQueue 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);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值