Java??????????-ArrayBlockingQueue
???????????? ???????[ 2016/1/18 10:12:49 ] ??????????????????? Java
??????????????£?д????????л?????????У?????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();
}
}
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11