生产者消费者问题(java实现)

1、生产者、消费者模型(若容器容量为1)
生产者线程:
if(a ==1) {
wait()
}
a++;
notify;

消费者:
if (a == 0) {
wait();
}
a--;
notify;

2、下面是一个代码实现(简单的缓存容量只有1,即:若容器中有一个了,就不能再生产了):
(1)最基本的写法:
资源类: (资源类或者共享对象一般都写成一个单独的类,然后在线程中实现共享)
public class Sample {
private int a;
public synchronized void increase() {
if (a != 0) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
a++;
notify();
System.out.println(a);
}

public synchronized void decrease() {
if (a == 0) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
a--;
notify();
System.out.println(a);
}
}
线程类:
生产者线程类:
public class IncreaseThread extends Thread {
private Sample sample;
public IncreaseThread(Sample sample) {
this.sample = sample;
}

@Override
public void run() {
while (true) {
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
sample.increase();
}
}
}
消费者线程类:
public class DecreaseThread extends Thread {
private Sample sample;
public DecreaseThread(Sample sample) {
this.sample = sample;
}

@Override
public void run() {
while (true) {
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
sample.decrease();
}
}
}
客户端测试类:
public class Test {
public static void main(String[] args) {
Sample sample = new Sample();
IncreaseThread increaseThread = new IncreaseThread(sample);
DecreaseThread decreaseThread = new DecreaseThread(sample);

increaseThread.start();
decreaseThread.start();
}
}
输出:
1
0
1
0
1
0

(2)这里存在一个问题,就是当有多个生产者和消费和消费者线程的时候,就会出现问题,输出的值并不一致是1 0 1 0。具体问题原因可以用代码具体分析一下。
解决方案:
资源类:
public class Sample {
private int a;
public synchronized void increase() {
while (a != 0) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
a++;
notify();
System.out.println(a);
}

public synchronized void decrease() {
while (a == 0) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
a--;
notify();
System.out.println(a);
}
}
客户端类
public class Test {
public static void main(String[] args) {
Sample sample = new Sample();
IncreaseThread increaseThread = new IncreaseThread(sample);
DecreaseThread decreaseThread = new DecreaseThread(sample);

IncreaseThread increaseThread2 = new IncreaseThread(sample);
DecreaseThread decreaseThread2= new DecreaseThread(sample);

increaseThread.start();
decreaseThread.start();

increaseThread2.start();
decreaseThread2.start();
}
}
说明,一般都用while;(这里用if代码片段,纯粹是为了过度)

3、代码升级,容器容量是number=5;
和上面相比,仅需修改如下代码:
public class Sample {
public static int number = 5;
private int a;
public synchronized void increase() {
while (a >= 5) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
a++;
notify();
System.out.println(a);
}

public synchronized void decrease() {
while (a <= 0) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
a--;
notify();
System.out.println(a);
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Java实现生产者消费者问题的示例代码: ```java import java.util.LinkedList; import java.util.Queue; import java.util.Random; import java.util.concurrent.Semaphore; public class ProducerConsumerExample { private static final int BUFFER_SIZE = 10; private static final Semaphore mutex = new Semaphore(1); private static final Semaphore empty = new Semaphore(BUFFER_SIZE); private static final Semaphore full = new Semaphore(0); private static final Queue<Integer> buffer = new LinkedList<>(); public static void main(String[] args) { Thread producer = new Thread(new Producer()); Thread consumer = new Thread(new Consumer()); producer.start(); consumer.start(); } static class Producer implements Runnable { private final Random random = new Random(); @Override public void run() { while (true) { try { int item = random.nextInt(); empty.acquire(); mutex.acquire(); buffer.add(item); System.out.println("Produced item: " + item); mutex.release(); full.release(); Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } } static class Consumer implements Runnable { @Override public void run() { while (true) { try { full.acquire(); mutex.acquire(); int item = buffer.remove(); System.out.println("Consumed item: " + item); mutex.release(); empty.release(); Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } } } ``` 上述代码中,我们使用了信号量来实现线程之间的同步和互斥。其中,`mutex`是一个二元信号量,用于实现互斥访问共享缓冲区;`empty`是一个计数信号量,表示缓冲区中空闲的位置数;`full`也是一个计数信号量,表示缓冲区中已经存储的数据项数。 在生产者线程中,我们首先使用`empty.acquire()`获取一个空闲位置,然后使用`mutex.acquire()`获取互斥锁,向缓冲区中添加一个数据项,最后使用`mutex.release()`释放互斥锁,使用`full.release()`增加已存储的数据项数。 在消费者线程中,我们首先使用`full.acquire()`获取一个已存储的数据项,然后使用`mutex.acquire()`获取互斥锁,从缓冲区中移除一个数据项,最后使用`mutex.release()`释放互斥锁,使用`empty.release()`增加空闲位置数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值