public static void main(String[] args) {
BlockingQueue<Integer> basketFileIndex = new ArrayBlockingQueue<Integer>(1);
String[] strArr = new String[]{"a","b","c","d"};
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < strArr.length; i++) {
try {
basketFileIndex.put(i);
System.out.println("存入:"+strArr[i]);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
boolean flag = false;
while(!flag){
try {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Integer index = basketFileIndex.take();
System.out.println("取出:"+strArr[index]);
if(index.intValue()==strArr.length-1){
System.out.println("完成");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
BlockingQueue的简单使用----控制线程数量
最新推荐文章于 2023-06-11 11:38:29 发布