范例1:
package com.contoso;
public class App {
public static void main(String[] args) {
Processor processor = new Processor();
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
try {
processor.produce();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
try {
processor.consume();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
t1.start();
t2.start();
try {
Thread.sleep(5000); // 让主线程暂停5秒
System.exit(0); // 强制退出死循环应用程序
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
package com.contoso;
import java.util.LinkedList;
import java.util.Random;
public class Processor {
private LinkedList<Integer> list = new LinkedList<>();
private final int LIMIT = 10;
private final Object lock = new Object();
public void produce() throws InterruptedException {
int value = 0;
while (true) {
synchronized (lock) {
while (list.size() == LIMIT) {
lock.wait();
}
list.add(value);
System.out.println("Producer added: " + value + " queue size is " + list.size());
value++;
lock.notify();
}
}
}
public void consume() throws InterruptedException {
Random random = new Random();
while (true) {
synchronized (lock) {
while (list.size() == 0) {
lock.wait();
}
int value = list.removeFirst();
System.out.print("Removed value by consumer is: " + value);
System.out.println(" Now list size is: " + list.size());
lock.notify();
}
Thread.sleep(random.nextInt(1000)); //force producer fill the queue to LIMIT_SIZE
}
}
}
run:
Producer added: 0 queue size is 1
Removed value by consumer is: 0 Now list size is: 0
Producer added: 1 queue size is 1
Producer added: 2 queue size is 2
Producer added: 3 queue size is 3
Producer added: 4 queue size is 4
Producer added: 5 queue size is 5
Producer added: 6 queue size is 6
Producer added: 7 queue size is 7
Producer added: 8 queue size is 8
Producer added: 9 queue size is 9
Producer added: 10 queue size is 10
Removed value by consumer is: 1 Now list size is: 9
Producer added: 11 queue size is 10
Removed value by consumer is: 2 Now list size is: 9
Producer added: 12 queue size is 10
Removed value by consumer is: 3 Now list size is: 9
Producer added: 13 queue size is 10
Removed value by consumer is: 4 Now list size is: 9
Producer added: 14 queue size is 10
Removed value by consumer is: 5 Now list size is: 9
Producer added: 15 queue size is 10
Removed value by consumer is: 6 Now list size is: 9
Producer added: 16 queue size is 10
Removed value by consumer is: 7 Now list size is: 9
Producer added: 17 queue size is 10
Removed value by consumer is: 8 Now list size is: 9
Producer added: 18 queue size is 10
Removed value by consumer is: 9 Now list size is: 9
Producer added: 19 queue size is 10
Removed value by consumer is: 10 Now list size is: 9
Producer added: 20 queue size is 10
Removed value by consumer is: 11 Now list size is: 9
Producer added: 21 queue size is 10
BUILD SUCCESSFUL (total time: 5 seconds)
范例2:
package com.contoso;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Random;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
class BlockingQueue<T> {
private Queue<T> queue = new LinkedList<>();
private int capacity;
private Lock lock = new ReentrantLock();
//condition variables
private Condition notFull = lock.newCondition();
private Condition notEmpty = lock.newCondition();
public BlockingQueue(int capacity) {
this.capacity = capacity;
}
public void put(T element) throws InterruptedException {
lock.lock();
try {
while (queue.size() == capacity) {
System.out.println("queue is full cannot put");
notFull.await(); //releases lock
}
queue.add(element);
System.out.println("Added to the queue " + element);
notEmpty.signal(); //calls waiting thread on the same object
} finally {
lock.unlock();
}
}
public T take() throws InterruptedException {
lock.lock();
try {
while (queue.isEmpty()) {
System.out.println("queue is empty, cannot take");
notEmpty.await(); //releases lock
}
T item = queue.remove();
System.out.println("Removed to the queue " + item);
notFull.signal(); //calls waiting thread on same object
return item;
} finally {
lock.unlock();
}
}
}
public class BlockingQueueApp {
public static void main(String[] args) {
BlockingQueue<Integer> blockingQueue = new BlockingQueue<>(10);
Random random = new Random();
Thread t1 = new Thread(new Runnable() {
public void run() {
try {
while (true) {
blockingQueue.put(random.nextInt(10));
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
Thread t2 = new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(1000);//wait for putting to the queue first
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
while (true) {
blockingQueue.take();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
t1.start();
t2.start();
try {
Thread.sleep(1200); // 让主线程暂停1.2秒
System.exit(0); // 强制退出死循环应用程序
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
run:
Added to the queue 4
Added to the queue 4
Added to the queue 7
Added to the queue 4
Added to the queue 4
Added to the queue 3
Added to the queue 5
Added to the queue 6
Added to the queue 5
Added to the queue 1
queue is full cannot put
Removed to the queue 4
Removed to the queue 4
Removed to the queue 7
Removed to the queue 4
Removed to the queue 4
Removed to the queue 3
Removed to the queue 5
Removed to the queue 6
Removed to the queue 5
Removed to the queue 1
queue is empty, cannot take
Added to the queue 0
Added to the queue 6
Added to the queue 1
Added to the queue 4
Added to the queue 2
Added to the queue 1
Added to the queue 4
Removed to the queue 0
Removed to the queue 6
Removed to the queue 1
Removed to the queue 4
Removed to the queue 2
Removed to the queue 1
Removed to the queue 4
queue is empty, cannot take
Added to the queue 2
Added to the queue 0
Added to the queue 7
Added to the queue 0
Added to the queue 5
Added to the queue 5
Added to the queue 9
Added to the queue 9
Added to the queue 8
Added to the queue 1
queue is full cannot put
Removed to the queue 2
Removed to the queue 0
Removed to the queue 7
Removed to the queue 0
Removed to the queue 5
Removed to the queue 5
Removed to the queue 9
Removed to the queue 9
Removed to the queue 8
Removed to the queue 1
queue is empty, cannot take
Added to the queue 4
Added to the queue 1
Added to the queue 0
Added to the queue 8
Added to the queue 3
Added to the queue 4
Added to the queue 7
Added to the queue 2
Added to the queue 2
Added to the queue 2
queue is full cannot put
Removed to the queue 4
Removed to the queue 1
Removed to the queue 0
Removed to the queue 8
Removed to the queue 3
Removed to the queue 4
Removed to the queue 7
Removed to the queue 2
Removed to the queue 2
Removed to the queue 2
queue is empty, cannot take
Added to the queue 2
Added to the queue 8
Added to the queue 9
Added to the queue 9
Added to the queue 2
Added to the queue 7
Added to the queue 6
Added to the queue 7
Added to the queue 3
Added to the queue 2
queue is full cannot put
Removed to the queue 2
Removed to the queue 8
Removed to the queue 9
Removed to the queue 9
Removed to the queue 2
Removed to the queue 7
Removed to the queue 6
Removed to the queue 7
Removed to the queue 3
Removed to the queue 2
queue is empty, cannot take
Added to the queue 8
Added to the queue 6
Added to the queue 9
Added to the queue 6
Added to the queue 6
Added to the queue 5
Added to the queue 0
Added to the queue 8
Added to the queue 0
Added to the queue 9
queue is full cannot put
Removed to the queue 8
Removed to the queue 6
Removed to the queue 9
Removed to the queue 6
Removed to the queue 6
Removed to the queue 5
Removed to the queue 0
Removed to the queue 8
Removed to the queue 0
Removed to the queue 9
queue is empty, cannot take
Added to the queue 6
Added to the queue 7
Added to the queue 2
Added to the queue 1
Added to the queue 5
Added to the queue 7
Added to the queue 5
Added to the queue 7
Added to the queue 3
Added to the queue 2
queue is full cannot put
Removed to the queue 6
Removed to the queue 7
Removed to the queue 2
Removed to the queue 1
Removed to the queue 5
Removed to the queue 7
Removed to the queue 5
Removed to the queue 7
Removed to the queue 3
Removed to the queue 2
queue is empty, cannot take
Added to the queue 8
Added to the queue 3
Added to the queue 7
Added to the queue 0
Added to the queue 7
Added to the queue 8
Added to the queue 8
Added to the queue 2
Added to the queue 8
Added to the queue 3
queue is full cannot put
Removed to the queue 8
Removed to the queue 3
Removed to the queue 7
Removed to the queue 0
Removed to the queue 7
Removed to the queue 8
Removed to the queue 8
Removed to the queue 2
Removed to the queue 8
Removed to the queue 3
queue is empty, cannot take
Added to the queue 9
Added to the queue 6
Added to the queue 2
Added to the queue 9
Added to the queue 2
Added to the queue 6
Added to the queue 9
Added to the queue 6
Added to the queue 0
Added to the queue 7
queue is full cannot put
Removed to the queue 9
Removed to the queue 6
Removed to the queue 2
Removed to the queue 9
Removed to the queue 2
Removed to the queue 6
Removed to the queue 9
Removed to the queue 6
Removed to the queue 0
Removed to the queue 7
queue is empty, cannot take
Added to the queue 2
Added to the queue 7
Added to the queue 7
Added to the queue 9
Added to the queue 4
Added to the queue 7
Added to the queue 9
Added to the queue 0
Added to the queue 6
Added to the queue 5
queue is full cannot put
Removed to the queue 2
Removed to the queue 7
Removed to the queue 7
Removed to the queue 9
Removed to the queue 4
Removed to the queue 7
Added to the queue 0
Added to the queue 3
Added to the queue 5
Added to the queue 2
Added to the queue 5
Added to the queue 1
queue is full cannot put
Removed to the queue 9
Removed to the queue 0
Removed to the queue 6
Removed to the queue 5
Removed to the queue 0
Removed to the queue 3
Removed to the queue 5
Removed to the queue 2
Removed to the queue 5
Removed to the queue 1
queue is empty, cannot take
Added to the queue 0
Added to the queue 4
Added to the queue 9
Added to the queue 6
Added to the queue 5
Added to the queue 0
Added to the queue 0
Added to the queue 2
Added to the queue 5
Added to the queue 2
queue is full cannot put
Removed to the queue 0
Removed to the queue 4
Removed to the queue 9
Removed to the queue 6
Removed to the queue 5
Removed to the queue 0
Removed to the queue 0
Removed to the queue 2
Removed to the queue 5
Removed to the queue 2
queue is empty, cannot take
Added to the queue 1
Added to the queue 8
Added to the queue 4
Added to the queue 2
Added to the queue 3
Removed to the queue 1
Removed to the queue 8
Removed to the queue 4
Removed to the queue 2
Removed to the queue 3
queue is empty, cannot take
Added to the queue 5
Added to the queue 6
Added to the queue 3
Added to the queue 2
Added to the queue 9
Added to the queue 3
Added to the queue 8
Added to the queue 6
Added to the queue 5
Removed to the queue 5
Removed to the queue 6
Removed to the queue 3
Removed to the queue 2
Removed to the queue 9
Removed to the queue 3
Removed to the queue 8
Removed to the queue 6
Removed to the queue 5
queue is empty, cannot take
Added to the queue 5
Added to the queue 5
Added to the queue 5
Added to the queue 2
Removed to the queue 5
Removed to the queue 5
Removed to the queue 5
Removed to the queue 2
queue is empty, cannot take
Added to the queue 7
Added to the queue 7
Added to the queue 8
Added to the queue 0
Added to the queue 6
Added to the queue 5
Removed to the queue 7
Removed to the queue 7
Removed to the queue 8
Removed to the queue 0
Removed to the queue 6
Removed to the queue 5
queue is empty, cannot take
Added to the queue 4
Removed to the queue 4
queue is empty, cannot take
Added to the queue 6
Added to the queue 4
Added to the queue 0
Added to the queue 7
Removed to the queue 6
Removed to the queue 4
Removed to the queue 0
Removed to the queue 7
queue is empty, cannot take
Added to the queue 4
Added to the queue 4
Added to the queue 7
Added to the queue 3
Added to the queue 7
Added to the queue 5
Added to the queue 8
Added to the queue 8
Added to the queue 2
Added to the queue 8
queue is full cannot put
Removed to the queue 4
Removed to the queue 4
Removed to the queue 7
Removed to the queue 3
Removed to the queue 7
Removed to the queue 5
Removed to the queue 8
Removed to the queue 8
Removed to the queue 2
Removed to the queue 8
queue is empty, cannot take
Added to the queue 2
Added to the queue 5
Added to the queue 2
Added to the queue 3
Added to the queue 5
Added to the queue 9
Added to the queue 5
Added to the queue 8
Added to the queue 5
Added to the queue 6
queue is full cannot put
Removed to the queue 2
Removed to the queue 5
Removed to the queue 2
Removed to the queue 3
Removed to the queue 5
Added to the queue 0
Added to the queue 0
Added to the queue 1
Added to the queue 9
Added to the queue 9
queue is full cannot put
Removed to the queue 9
Removed to the queue 5
Removed to the queue 8
Removed to the queue 5
Removed to the queue 6
Removed to the queue 0
Removed to the queue 0
Removed to the queue 1
Removed to the queue 9
Removed to the queue 9
queue is empty, cannot take
Added to the queue 3
Added to the queue 9
Added to the queue 5
Added to the queue 7
Added to the queue 7
Added to the queue 2
Added to the queue 3
Added to the queue 9
Removed to the queue 3
Removed to the queue 9
Removed to the queue 5
Removed to the queue 7
Removed to the queue 7
Removed to the queue 2
Removed to the queue 3
Removed to the queue 9
queue is empty, cannot take
Added to the queue 1
Added to the queue 2
Added to the queue 6
Added to the queue 3
Added to the queue 6
Added to the queue 2
Added to the queue 2
Added to the queue 7
Added to the queue 4
Added to the queue 0
queue is full cannot put
Removed to the queue 1
Removed to the queue 2
Removed to the queue 6
Added to the queue 9
Added to the queue 1
Added to the queue 1
queue is full cannot put
Removed to the queue 3
Added to the queue 0
queue is full cannot put
Removed to the queue 6
Added to the queue 6
queue is full cannot put
Removed to the queue 2
Removed to the queue 2
Removed to the queue 7
Removed to the queue 4
Removed to the queue 0
Removed to the queue 9
Removed to the queue 1
Removed to the queue 1
Removed to the queue 0
Removed to the queue 6
Added to the queue 9
Added to the queue 7
Added to the queue 9
Added to the queue 8
Added to the queue 9
Removed to the queue 9
Added to the queue 7
Added to the queue 5
Added to the queue 2
Added to the queue 1
Added to the queue 0
Added to the queue 1
queue is full cannot put
Removed to the queue 7
Added to the queue 1
queue is full cannot put
Removed to the queue 9
Added to the queue 8
queue is full cannot put
Removed to the queue 8
Added to the queue 5
Removed to the queue 9
Added to the queue 9
queue is full cannot put
Removed to the queue 7
Added to the queue 5
queue is full cannot put
Removed to the queue 5
Added to the queue 4
queue is full cannot put
Removed to the queue 2
Added to the queue 8
queue is full cannot put
Removed to the queue 1
Removed to the queue 0
Added to the queue 3
Added to the queue 7
queue is full cannot put
Removed to the queue 1
Added to the queue 9
Removed to the queue 1
Added to the queue 9
queue is full cannot put
Removed to the queue 8
Removed to the queue 5
Removed to the queue 9
Removed to the queue 5
Removed to the queue 4
Removed to the queue 8
Removed to the queue 3
Removed to the queue 7
Removed to the queue 9
Removed to the queue 9
Added to the queue 1
Added to the queue 7
Added to the queue 5
Added to the queue 7
Added to the queue 7
Added to the queue 2
Added to the queue 3
Added to the queue 3
Added to the queue 8
Removed to the queue 1
Added to the queue 0
Added to the queue 9
queue is full cannot put
Removed to the queue 7
Added to the queue 5
queue is full cannot put
Removed to the queue 5
Added to the queue 4
Removed to the queue 7
Removed to the queue 7
Removed to the queue 2
Removed to the queue 3
Removed to the queue 3
Added to the queue 1
Removed to the queue 8
Removed to the queue 0
Removed to the queue 9
Removed to the queue 5
Removed to the queue 4
Removed to the queue 1
queue is empty, cannot take
Added to the queue 2
Added to the queue 7
Added to the queue 3
Added to the queue 4
Added to the queue 4
Added to the queue 5
Added to the queue 9
Added to the queue 8
Added to the queue 1
Added to the queue 3
queue is full cannot put
Removed to the queue 2
Removed to the queue 7
Added to the queue 7
Added to the queue 6
queue is full cannot put
Removed to the queue 3
Removed to the queue 4
Removed to the queue 4
Removed to the queue 5
Removed to the queue 9
Added to the queue 5
Added to the queue 8
Added to the queue 4
Added to the queue 5
Added to the queue 8
queue is full cannot put
Removed to the queue 8
Removed to the queue 1
Removed to the queue 3
Removed to the queue 7
Removed to the queue 6
Removed to the queue 5
Removed to the queue 8
Removed to the queue 4
Removed to the queue 5
Added to the queue 1
Added to the queue 2
Added to the queue 1
Added to the queue 2
Added to the queue 2
Added to the queue 4
Added to the queue 4
Added to the queue 3
Added to the queue 9
queue is full cannot put
Removed to the queue 8
Added to the queue 2
queue is full cannot put
Removed to the queue 1
Added to the queue 0
queue is full cannot put
Removed to the queue 2
Added to the queue 6
queue is full cannot put
Removed to the queue 1
Added to the queue 2
queue is full cannot put
Removed to the queue 2
Added to the queue 2
queue is full cannot put
Removed to the queue 2
Removed to the queue 4
Removed to the queue 4
Removed to the queue 3
Removed to the queue 9
Removed to the queue 2
Added to the queue 2
Added to the queue 4
Added to the queue 4
Added to the queue 0
Added to the queue 5
Added to the queue 6
queue is full cannot put
Removed to the queue 0
Removed to the queue 6
Added to the queue 4
Added to the queue 8
queue is full cannot put
Removed to the queue 2
Added to the queue 8
queue is full cannot put
Removed to the queue 2
Removed to the queue 2
Removed to the queue 4
Removed to the queue 4
Removed to the queue 0
Removed to the queue 5
Removed to the queue 6
Added to the queue 5
Removed to the queue 4
Added to the queue 5
Added to the queue 3
Added to the queue 2
Added to the queue 9
Added to the queue 3
Added to the queue 6
Added to the queue 6
queue is full cannot put
Removed to the queue 8
Added to the queue 4
queue is full cannot put
Removed to the queue 8
Removed to the queue 5
Removed to the queue 5
Removed to the queue 3
Added to the queue 2
Removed to the queue 2
Removed to the queue 9
Removed to the queue 3
Removed to the queue 6
Removed to the queue 6
Removed to the queue 4
Added to the queue 4
Added to the queue 7
Added to the queue 6
Added to the queue 0
Removed to the queue 2
Added to the queue 6
Added to the queue 1
Added to the queue 5
Added to the queue 8
Added to the queue 0
Added to the queue 1
queue is full cannot put
Removed to the queue 4
Removed to the queue 7
Removed to the queue 6
Removed to the queue 0
Removed to the queue 6
Removed to the queue 1
Removed to the queue 5
Removed to the queue 8
Removed to the queue 0
Removed to the queue 1
queue is empty, cannot take
Added to the queue 6
Added to the queue 2
Added to the queue 3
Added to the queue 9
Added to the queue 8
Added to the queue 9
Added to the queue 5
Added to the queue 8
Added to the queue 9
Added to the queue 0
Removed to the queue 6
Removed to the queue 2
Removed to the queue 3
Removed to the queue 9
Removed to the queue 8
Removed to the queue 9
Removed to the queue 5
Removed to the queue 8
Removed to the queue 9
Removed to the queue 0
queue is empty, cannot take
Added to the queue 4
Added to the queue 5
Added to the queue 7
Added to the queue 9
Added to the queue 4
Added to the queue 3
Added to the queue 6
Removed to the queue 4
Added to the queue 0
Added to the queue 7
Added to the queue 3
Added to the queue 4
queue is full cannot put
Removed to the queue 5
Added to the queue 1
queue is full cannot put
Removed to the queue 7
Removed to the queue 9
Removed to the queue 4
Removed to the queue 3
Removed to the queue 6
Removed to the queue 0
Removed to the queue 7
Removed to the queue 3
Removed to the queue 4
Removed to the queue 1
Added to the queue 1
Added to the queue 5
Added to the queue 4
Added to the queue 0
Added to the queue 4
Added to the queue 7
Added to the queue 6
Added to the queue 9
Added to the queue 9
Added to the queue 1
queue is full cannot put
Removed to the queue 1
Removed to the queue 5
Removed to the queue 4
Removed to the queue 0
Removed to the queue 4
Added to the queue 9
Added to the queue 4
Added to the queue 5
Added to the queue 6
Added to the queue 5
queue is full cannot put
Removed to the queue 7
Removed to the queue 6
Removed to the queue 9
Removed to the queue 9
Added to the queue 2
Added to the queue 6
Added to the queue 6
Added to the queue 4
queue is full cannot put
Removed to the queue 1
Removed to the queue 9
Removed to the queue 4
Removed to the queue 5
Removed to the queue 6
Removed to the queue 5
Removed to the queue 2
Removed to the queue 6
Removed to the queue 6
Removed to the queue 4
queue is empty, cannot take
Added to the queue 4
Added to the queue 7
Added to the queue 9
Removed to the queue 4
Removed to the queue 7
Removed to the queue 9
queue is empty, cannot take
Added to the queue 6
Added to the queue 8
Added to the queue 4
Added to the queue 2
Removed to the queue 6
Removed to the queue 8
Removed to the queue 4
Removed to the queue 2
queue is empty, cannot take
Added to the queue 5
Added to the queue 8
Added to the queue 8
Added to the queue 1
Added to the queue 4
Added to the queue 9
Added to the queue 2
Added to the queue 5
Added to the queue 8
Added to the queue 5
queue is full cannot put
Removed to the queue 5
Removed to the queue 8
Removed to the queue 8
Removed to the queue 1
Removed to the queue 4
Removed to the queue 9
Removed to the queue 2
Removed to the queue 5
Removed to the queue 8
Removed to the queue 5
queue is empty, cannot take
Added to the queue 9
Added to the queue 4
Added to the queue 8
Added to the queue 1
Added to the queue 4
Added to the queue 0
Added to the queue 4
Added to the queue 7
Added to the queue 4
Added to the queue 1
queue is full cannot put
Removed to the queue 9
Removed to the queue 4
Removed to the queue 8
Removed to the queue 1
Removed to the queue 4
Removed to the queue 0
Removed to the queue 4
Removed to the queue 7
Removed to the queue 4
Removed to the queue 1
queue is empty, cannot take
Added to the queue 0
Added to the queue 5
Added to the queue 4
Added to the queue 6
Added to the queue 7
Added to the queue 8
Added to the queue 0
Added to the queue 2
Added to the queue 9
Added to the queue 7
queue is full cannot put
Removed to the queue 0
Removed to the queue 5
Removed to the queue 4
Removed to the queue 6
Removed to the queue 7
Removed to the queue 8
Removed to the queue 0
Removed to the queue 2
Removed to the queue 9
Removed to the queue 7
queue is empty, cannot take
Added to the queue 4
Added to the queue 9
Removed to the queue 4
Removed to the queue 9
queue is empty, cannot take
Added to the queue 2
Added to the queue 5
Added to the queue 3
Added to the queue 0
Added to the queue 4
Added to the queue 2
Added to the queue 8
Added to the queue 4
Added to the queue 5
Removed to the queue 2
Removed to the queue 5
Removed to the queue 3
Removed to the queue 0
Removed to the queue 4
Removed to the queue 2
Removed to the queue 8
Removed to the queue 4
Removed to the queue 5
queue is empty, cannot take
Added to the queue 5
Added to the queue 6
Added to the queue 0
Added to the queue 9
Added to the queue 0
Added to the queue 2
Added to the queue 3
Removed to the queue 5
Removed to the queue 6
Removed to the queue 0
Removed to the queue 9
Removed to the queue 0
Removed to the queue 2
Removed to the queue 3
queue is empty, cannot take
Added to the queue 1
Added to the queue 7
Added to the queue 1
Added to the queue 4
Added to the queue 0
Added to the queue 8
Added to the queue 8
Added to the queue 3
Added to the queue 9
Added to the queue 0
queue is full cannot put
Removed to the queue 1
Removed to the queue 7
Removed to the queue 1
Removed to the queue 4
Removed to the queue 0
范例3:
package com.contoso;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.LinkedBlockingQueue;
public class LinkedBlockingQueueExample1 {
/**
* Concurrent Queue interface.
*
* Implementations: LinkedBlockingQueue, ArrayBlockingQueue,
* PriorityBlockingQueue, SynchronizedQueue.
*
* Used for the Producer-Consumer pattern.
*
* Blocking methods: put/take; Timed blocking methods: offer, poll;
*
* Can be bounded or unbounded.
*
*/
public static void useBlockingQueue() {
System.out.println("=== BlockingQueue ===");
// Bounded UUID queue
LinkedBlockingQueue uuidQueue = new LinkedBlockingQueue<UUID>(10);
System.out.println("Queue will execute for 10s");
// Multiple consumers
Runnable runConsumer = () -> {
while (!Thread.currentThread().isInterrupted()) {
try {
Object uuid = uuidQueue.take();
System.err.println("Consumed: " + uuid + " by " + Thread.currentThread().getName());
} catch (InterruptedException e) {
// interrupted pattern
// InterruptedException makes isInterrupted returns false
Thread.currentThread().interrupt();
System.err.println("Consumer Finished");
}
}
};
Thread consumer1 = new Thread(runConsumer);
Thread consumer2 = new Thread(runConsumer);
consumer1.start();
consumer2.start();
// Producer Thread
Runnable runProducer = () -> {
try {
while (!Thread.currentThread().isInterrupted()) {
Random r = new Random();
// Delay producer
Thread.sleep(r.nextInt(1000));
UUID randomUUID = UUID.randomUUID();
System.out.println("Produced: " + randomUUID + " by " + Thread.currentThread().getName());
uuidQueue.put(randomUUID);
}
} catch (InterruptedException e) {
// interrupted pattern
System.err.println("Producer Finished");
}
};
// Multiple producers - Examples using simple threads this time.
Thread producer1 = new Thread(runProducer);
Thread producer2 = new Thread(runProducer);
Thread producer3 = new Thread(runProducer);
producer1.start();
producer2.start();
producer3.start();
try {
// Queue will run for 10secs
Thread.sleep(10000);
producer1.interrupt();
producer2.interrupt();
producer3.interrupt();
consumer1.interrupt();
consumer2.interrupt();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
useBlockingQueue();
}
}