生產者消費者--BlockingQueue和wait、notify兩種方式實現
阿新 • • 發佈:2019-02-14
阻塞佇列BlockingQueue實現
import java.util.concurrent.BlockingQueue;
public class Consumer implements Runnable {
/*
BlockingQueue put(e) 和 take() 這兩個方法是帶阻塞的。
*/
BlockingQueue<String> queue;
public Consumer(BlockingQueue queue) {
this.queue = queue;
}
@Override
public void run() {
try {
String tmp = queue.take();
System.out.println(Thread.currentThread().getName() + " have consumed a product from " + tmp);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
import java.util.concurrent.BlockingQueue;
public class Producer implements Runnable {
BlockingQueue<String> queue;
public Producer(BlockingQueue queue) {
this.queue = queue;
}
@Override
public void run() {
try {
String tmp = Thread.currentThread().getName();
System.out.println (Thread.currentThread().getName() + " have made a product");
queue.put(tmp);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
public class Main {
public static void main(String[] args) {
BlockingQueue<String> queue = new LinkedBlockingQueue<String>(2);
Producer producer = new Producer(queue);
Consumer consumer = new Consumer(queue);
for (int i = 0; i < 5; i++) {
new Thread(producer, "Producer" + (i + 1)).start();
new Thread(consumer, "Consumer" + (i + 1)).start();
}
}
}
輸出
Producer1 have made a product
Producer2 have made a product
Consumer2 have consumed a product from Producer1
Consumer1 have consumed a product from Producer2
Producer3 have made a product
Producer4 have made a product
Consumer4 have consumed a product from Producer3
Producer5 have made a product
Consumer5 have consumed a product from Producer4
Consumer3 have consumed a product from Producer5
synchronized關鍵字、wait、notify實現
import java.util.LinkedList;
import java.util.List;
public class Consumer implements Runnable {
private List<String> buffer = new LinkedList<String>();
private Integer capacity;
public Consumer(List buffer, Integer capacity) {
this.buffer = buffer;
this.capacity = capacity;
}
@Override
public void run() {
synchronized (capacity){
try {
if (buffer.isEmpty())
capacity.wait();
String tmp = buffer.remove(0);
System.out.println(Thread.currentThread().getName() + " have consume a product from " + tmp);
capacity.notifyAll();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
import java.util.LinkedList;
import java.util.List;
public class Producer implements Runnable {
private List<String> buffer = new LinkedList<String>();
private Integer capacity;
public Producer(List buffer, Integer capacity) {
this.buffer = buffer;
this.capacity = capacity;
}
@Override
public void run() {
synchronized (capacity){
try {
if (buffer.size() >= capacity)
capacity.wait();
buffer.add(Thread.currentThread().getName());
System.out.println(Thread.currentThread().getName() + " have made a product");
capacity.notifyAll();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
import java.util.LinkedList;
import java.util.List;
public class Main {
public static void main(String[] args) {
Integer capacity = 5;
List<String> buffer = new LinkedList<String>();
Producer producer = new Producer(buffer, capacity);
Consumer consumer = new Consumer(buffer, capacity);
for (int i = 1; i < 5; i++) {
new Thread(producer,"Producer" + i).start();
new Thread(consumer,"Consumer" + i).start();
}
}
}
輸出
Producer1 have made a product
Consumer1 have consume a product from Producer1
Producer2 have made a product
Producer3 have made a product
Producer4 have made a product
Consumer2 have consume a product from Producer2
Consumer4 have consume a product from Producer3
Consumer3 have consume a product from Producer4