public class TestArrayBlockingQueue {
public static void main(String[] args) throws InterruptedException {
BlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(3);
for(int i =0;i<2;i++){
final int num = i;
new Thread(new Runnable() {
@Override
public void run() {
while (true){
try {
queue.put(num);
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
Thread.sleep(100);
Integer a = queue.take();
System.out.println("a = " + a);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
ArrayBlockingQueue
最新推荐文章于 2025-02-02 22:05:40 发布