ConcurrentLinkedQueue 高效讀寫的隊列

//線程安全隊列,先進先出
ConcurrentLinkedQueue conLinkedQueue = new ConcurrentLinkedQueue();

for(int i = 0;i<100;i++){
   conLinkedQueue.add(i);//往隊列加數據
}

 

//循環隊列
while(!conLinkedQueue.isEmpty()){
    //彈出數據
    System.out.println("Thread Id :"+Thread.currentThread().getId()+" "+conLinkedQueue.poll());

    try {
        Thread.sleep(100);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
相關文章
相關標籤/搜索