java多線程之併發集合(BlockingQueue)

簡介

 

實現

package com.np.ota.test.queue;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

public class BlockQueueTest {

	public static void main(String[] args) throws InterruptedException {
		BlockingQueue<String> queue = new LinkedBlockingQueue<String>(Integer.MAX_VALUE);
		//queue.add("121");
		queue.offer("122", 2, TimeUnit.SECONDS);//添加元素,添加超時時間
		
		//Retrieves and removes the head of this queue, or returns null if this queue is empty.
		System.out.println(queue.poll());//獲取並刪除,不阻塞
		//Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.
		System.out.println(queue.peek());//獲取不刪除,不阻塞
		//Retrieves and removes the head of this queue, waiting if necessary until an element becomes available.
		System.out.println(queue.take());//獲取並刪除,阻塞
	}

}

結果

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章