多線程之阻塞隊列ArrayBlockingQueue,BlockingQueue,arrayblockingqueue
ArrayBlockingQueue是個有數組支援的有界的阻塞隊列。該隊列按照先進先出FIFO的原理對元素排序,插入新元素市場隊列的尾部,擷取新元素是操作隊列的開始處。一旦見了建立了緩衝區,就不能再增加其容量,試圖從已滿的隊列中方式元素會導致操作阻塞;試圖從空的隊列中提取元素將導致阻塞。
提拱了四種方法,只有put(),take()才會發生阻塞。
下面是阻塞隊列的例子。
package andy.thread.test;import java.util.concurrent.ArrayBlockingQueue;import java.util.concurrent.BlockingQueue;/** * @author Zhang,Tianyou * @version 2014年11月9日 下午5:29:28 */public class BlockingQueueTest {public static void main(String[] args) {final int CAPACITY = 3;BlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(CAPACITY);for (int i = 0; i < 2; i++) {new Thread() {public void run() {while (true) {try {Thread.sleep((long) (Math.random() * 1000));System.out.println(Thread.currentThread().getName()+ "準備放資料!");queue.put(1);System.out.println(Thread.currentThread().getName()+ "已經放了資料," + "隊列目前有" + queue.size()+ "個資料");} catch (InterruptedException e) {e.printStackTrace();}}}}.start();}new Thread() {public void run() {while (true) {try {// 將此處的睡眠時間分別改為100和1000,觀察運行結果Thread.sleep(1000);System.out.println(Thread.currentThread().getName()+ "準備取資料!");queue.take();System.out.println(Thread.currentThread().getName()+ "已經取走資料," + "隊列目前有" + queue.size() + "個資料");} catch (InterruptedException e) {e.printStackTrace();}}}}.start();}}
定義3各大小的阻塞隊列,定義了兩個線程,一個取,一個存。執行結果如下:
Thread-0準備放資料!
Thread-0已經放了資料,隊列目前有1個資料
Thread-1準備放資料!
Thread-1已經放了資料,隊列目前有2個資料
Thread-0準備放資料!
Thread-0已經放了資料,隊列目前有3個資料
Thread-1準備放資料!
Thread-0準備放資料!
Thread-2準備取資料!
Thread-2已經取走資料,隊列目前有2個資料
Thread-1已經放了資料,隊列目前有3個資料
2、 可以使用阻塞隊列完成兩個線程迴圈交替列印。
我們需要定義兩個大小唯一的阻塞隊列,當一個線程執行時,給另一個隊列裡存一個值,讓另一個線程處於阻塞狀態;當前線程執行完畢時,將另一個阻塞隊列取出值,讓對應的線程執行。
執行個體如下:
package andy.thread.test;import java.util.concurrent.ArrayBlockingQueue;import java.util.concurrent.BlockingQueue;/** * @author Zhang,Tianyou * @version 2014年11月9日 下午5:43:28 */public class BlockingQueueCommunication {public static void main(String[] args) {A a = new A();new Thread(new Runnable() {@Overridepublic void run() {for (int i = 1; i <= 3; i++) {a.sub(i);}}}).start();for (int i = 1; i <= 3; i++) {a.main(i);}}static class A {BlockingQueue<Integer> mainQueue = new ArrayBlockingQueue<Integer>(1);BlockingQueue<Integer> subQueue = new ArrayBlockingQueue<Integer>(1);// 定義一個匿名建構函式 匿名建構函式在所有的建構函式前開始執行 new多少個執行個體執行多少次// static 是在JVM載入時執行 執行一次{try {subQueue.put(1);// 阻塞子線程 先讓第主線程執行} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public void sub(int i) {try {// 阻塞主線程subQueue.put(1);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}System.out.println("sub thread task is " + i);try {mainQueue.take(); // 子線程執行完 讓改主線程執行} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public void main(int i) {try {// 阻塞主線程mainQueue.put(1);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}System.out.println("main thread task is " + i);try {subQueue.take(); // 子線程執行完 讓改主線程執行} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}
運行效果如下
main thread task is 1
sub thread task is 1
main thread task is 2
sub thread task is 2
main thread task is 3
sub thread task is 3