BlockingQueue
BlockingQueue也是java.util.concurrent下的主要用來控制線程同步的工具油啤。
-
主要的方法是:put、take一對(duì)阻塞存取;add、poll一對(duì)非阻塞存取歪沃。
- 插入:
- add(anObject):把a(bǔ)nObject加到BlockingQueue里,即如果BlockingQueue可以容納,則返回true,否則拋出異常,不好
- offer(anObject):表示如果可能的話,將anObject加到BlockingQueue里,即如果BlockingQueue可以容納,則返回true,否則返回false.
- put(anObject):把a(bǔ)nObject加到BlockingQueue里,如果BlockQueue沒有空間,則調(diào)用此方法的線程被阻斷直到BlockingQueue里面有空間再繼續(xù), 有阻塞, 放不進(jìn)去就等待
- add(anObject):把a(bǔ)nObject加到BlockingQueue里,即如果BlockingQueue可以容納,則返回true,否則拋出異常,不好
- 讀取:
- poll(time):取走BlockingQueue里排在首位的對(duì)象,若不能立即取出,則可以等time參數(shù)規(guī)定的時(shí)間,取不到時(shí)返回null; 取不到返回null
- take():取走BlockingQueue里排在首位的對(duì)象,若BlockingQueue為空,阻斷進(jìn)入等待狀態(tài)直到Blocking有新的對(duì)象被加入為止; 阻塞, 取不到就一直等 - 其他
- int remainingCapacity();返回隊(duì)列剩余的容量嫌松,在隊(duì)列插入和獲取的時(shí)候沪曙,數(shù)據(jù)可能不準(zhǔn), 不能保證數(shù)據(jù)的準(zhǔn)確性
- boolean remove(Object o); 從隊(duì)列移除元素,如果存在萎羔,即移除一個(gè)或者更多液走,隊(duì)列改 變了返回true
- public boolean contains(Object o); 查看隊(duì)列是否存在這個(gè)元素,存在返回true
- int drainTo(Collection<? super E> c); //移除此隊(duì)列中所有可用的元素,并將它們添加到給定 collection 中。取出放到集合中
- int drainTo(Collection<? super E> c, int maxElements); 和上面方法的區(qū)別在于育灸,指定了移 動(dòng)的數(shù)量; 取出指定個(gè)數(shù)放到集合
- 插入:
-
BlockingQueue有四個(gè)具體的實(shí)現(xiàn)類,常用的兩種實(shí)現(xiàn)類為:
-
ArrayBlockingQueue:
- 一個(gè)由數(shù)組支持的有界阻塞隊(duì)列腻窒,規(guī)定大小的BlockingQueue,其構(gòu)造函數(shù)必須帶一個(gè)int參數(shù)來指明其大小.其所含的對(duì)象是以FIFO(先入先出)順序排序的。
-
LinkedBlockingQueue:
- 大小不定的BlockingQueue,若其構(gòu)造函數(shù)帶一個(gè)規(guī)定大小的參數(shù),生成的BlockingQueue有大小限制,若不帶大小參數(shù),所生成的BlockingQueue的大小由Integer.MAX_VALUE來決定.其所含的對(duì)象是以FIFO(先入先出)順序排序的磅崭。
- LinkedBlockingQueue 可以指定容量儿子,也可以不指定,不指定的話砸喻,默認(rèn)最大是Integer.MAX_VALUE,其中主要用到put和take方法柔逼,put方法在隊(duì)列滿的時(shí)候會(huì)阻塞直到有隊(duì)列成員被消費(fèi),take方法在隊(duì)列空的時(shí)候會(huì)阻塞割岛,直到有隊(duì)列成員被放進(jìn)來愉适。
-
LinkedBlockingQueue和ArrayBlockingQueue區(qū)別:
- LinkedBlockingQueue和ArrayBlockingQueue比較起來,它們背后所用的數(shù)據(jù)結(jié)構(gòu)不一樣,導(dǎo)致LinkedBlockingQueue的數(shù)據(jù)吞吐量要大于ArrayBlockingQueue,但在線程數(shù)量很大時(shí)其性能的可預(yù)見性低于ArrayBlockingQueue.
-
生產(chǎn)者代碼
import java.util.Random;
import java.util.concurrent.BlockingQueue;
public class TestBlockingQueueProducer implements Runnable {
BlockingQueue<String> queue;
Random random = new Random();
public TestBlockingQueueProducer(BlockingQueue<String> queue) {
this.queue = queue;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
try {
Thread.sleep(random.nextInt(10));
String task = Thread.currentThread().getName() + " made a product " + i;
System.out.println(task);
queue.put(task);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
消費(fèi)者代碼
mport java.util.Random;
import java.util.concurrent.BlockingQueue;
public class TestBlockingQueueConsumer implements Runnable{
BlockingQueue<String> queue;
Random random = new Random();
public TestBlockingQueueConsumer(BlockingQueue<String> queue){
this.queue = queue;
}
@Override
public void run() {
try {
Thread.sleep(random.nextInt(10));
System.out.println(Thread.currentThread().getName()+ "trying...");
String temp = queue.take();//如果隊(duì)列為空,會(huì)阻塞當(dāng)前線程
System.out.println(Thread.currentThread().getName() + " get a job " +temp);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
測(cè)試代碼
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
public class TestBlockingQueue {
public static void main(String[] args) {
BlockingQueue<String> queue = new LinkedBlockingQueue<String>(2);
// BlockingQueue<String> queue = new LinkedBlockingQueue<String>();
// 不設(shè)置的話癣漆,LinkedBlockingQueue默認(rèn)大小為Integer.MAX_VALUE
// BlockingQueue<String> queue = new ArrayBlockingQueue<String>(2);
TestBlockingQueueConsumer consumer = new TestBlockingQueueConsumer(queue);
TestBlockingQueueProducer producer = new TestBlockingQueueProducer(queue);
for (int i = 0; i < 3; i++) {
new Thread(producer, "Producer" + (i + 1)).start();
}
for (int i = 0; i < 5; i++) {
new Thread(consumer, "Consumer" + (i + 1)).start();
}
new Thread(producer, "Producer" + (5)).start();
}
}
?