??????????????£?д????????л?????????У?????JAVA??Lock?μ?Condition?????????JAVA?????????????????ArrayBlockingQueue??
????ArrayBlockingQueue?????????????н????????У??ζ??а???FIFO???????????????????????????????????????????????????????????????????
????ArrayBlockingQueue???????????????????

???????ArrayBlockingQueue?????????BlockingQueue???????????????У???????put????????take????????????take?????????????put???
????????????????????
import java.util.Random;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
/**
* ???????????
* BlockingQueue
* ?????????????????
* @author
*
*/
public class BlockingQueueCommunicationTest {
public static void main(String[] args) {
final BlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(3);
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
Thread.sleep(new Random().nextInt(1000));
System.out.println("???"
+ Thread.currentThread().getName() + "???????");
queue.put(1);
System.out.println("???"
+ Thread.currentThread().getName() + "?????????????"
+ queue.size() + "??");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
Thread.sleep(new Random().nextInt(1000));
System.out.println("???"
+ Thread.currentThread().getName() + "??????");
queue.take();
System.out.println("???"
+ Thread.currentThread().getName() + "?????????????"
+ queue.size() + "??");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
}