JAVA解决生产消费者_Java常用三种方式解决生产者消费者问题(详细)
package test;
/**
* Synchronized 版本解決生產者消費者
* wait() / notify()方法
*/
import java.util.LinkedList;
import java.util.Queue;
public class ProducerAndConsumerForSynchronized {
// 1. 先定義最大生產長度
private final int MAX_SIZE = 10;
// 2.定義儲存隊列
private QueuestoreList = new LinkedList();
// 3.定義生產者
class Producer extends Thread {
@Override
public void run() {
producer();
}
private void producer() {
while (true) {
synchronized (storeList) {
while (storeList.size() == MAX_SIZE) {
storeList.notify();
System.out.println("當前庫存數量達到最大值:" + MAX_SIZE + "條");
try {
// 暫停生產
storeList.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
storeList.add(new Object());
storeList.notify();
System.out.println("生產者產量增加一條,庫存" + storeList.size());
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
// 4.定義消費者
class Consumer extends Thread {
@Override
public void run() {
consumer();
}
private void consumer() {
while (true) {
synchronized (storeList) {
while (storeList.size() == 0) {
storeList.notify();
System.out.println("當前庫存為空");
try {
// 暫停消費
storeList.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
storeList.poll();
storeList.notify();
System.out.println("消費者消費一條產量,當前庫存為" + storeList.size());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
public static void main(String[] args) {
ProducerAndConsumerForSynchronized pc = new ProducerAndConsumerForSynchronized();
Producer producer = pc.new Producer();
Consumer consumer = pc.new Consumer();
producer.start();
consumer.start();
}
}
package test;
/**
* Lock 版本解決生產者消費者
* await() / signal()方法
*/
import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class ProducerAndConsumerForLock {
// 1. 先定義最大生產長度
private final int MAX_SIZE = 10;
// 2.定義儲存隊列
private QueuestoreList = new LinkedList();
private final Lock lock = new ReentrantLock();
private final Condition condition = lock.newCondition();
class Producer extends Thread {
@Override
public void run() {
producer();
}
private void producer() {
while (true) {
lock.lock();
try {
while (storeList.size() == MAX_SIZE) {
System.out.println("當前庫存滿");
try {
condition.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
storeList.add(1);
condition.signal();
System.out.println("生產者產量增加一條,庫存" + storeList.size());
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
} finally {
lock.unlock();
}
}
}
}
class Consumer extends Thread {
@Override
public void run() {
consumer();
}
private void consumer() {
while (true) {
lock.lock();
try {
while (storeList.size() == 0) {
System.out.println("當前庫存為空");
try {
condition.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
storeList.poll();
condition.signal();
System.out.println("消費者消費一條任務,當前隊列長度為" + storeList.size());
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
} finally {
lock.unlock();
}
}
}
}
public static void main(String[] args) {
ProducerAndConsumerForLock pc = new ProducerAndConsumerForLock();
Producer producer = pc.new Producer();
Consumer consumer = pc.new Consumer();
producer.start();
consumer.start();
}
}
package test;
/**
* BlockingQueue阻塞隊列版本解決生產者消費者
* put() / take() 方法
*/
import java.util.concurrent.LinkedBlockingQueue;
public class ProducerAndConsumerForBlockingQueue {
// 定義儲存隊列
private LinkedBlockingQueuestoreList = new LinkedBlockingQueue<>(10);
class Producer extends Thread {
@Override
public void run() {
produce();
}
public void produce() {
while (true) {
try {
storeList.put(new Object());
System.out.println("生產者生產了一個商品 ,庫存:" + storeList.size());
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class Consumer extends Thread {
@Override
public void run() {
consume();
}
public void consume() {
while (true) {
try {
storeList.take();
System.out.println("消費者消費了一個商品 ,庫存:" + storeList.size());
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
ProducerAndConsumerForBlockingQueue pc = new ProducerAndConsumerForBlockingQueue();
Producer producer = pc.new Producer();
Consumer consumer = pc.new Consumer();
producer.start();
consumer.start();
}
}
總結
以上是生活随笔為你收集整理的JAVA解决生产消费者_Java常用三种方式解决生产者消费者问题(详细)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 喝什么蔬菜汁能减肥
- 下一篇: java对象类型转换分为_java-如何