日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

Java多线程之线程并发库阻塞队列的应用

發(fā)布時(shí)間:2025/3/20 java 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java多线程之线程并发库阻塞队列的应用 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

ArrayBlockingQueue(jdk中已經(jīng)提供 就在那個(gè)condition類說明里的可阻塞示例程序的下面就說明了)
注意三個(gè)添加方法的區(qū)別->查API文檔 拿插入來說 一個(gè)會(huì)拋異常 一個(gè)返回true/false 一個(gè)會(huì)阻塞
是記不住的 找到doc即可 把精力留出來吧
阻塞隊(duì)列與Semaphore有些相似,但也不同,阻塞隊(duì)列是一方存放數(shù)據(jù),另一方釋放數(shù)據(jù),Semaphore通常則是由同一方設(shè)置和釋放信號(hào)量。

用3個(gè)空間的隊(duì)列來演示阻塞隊(duì)列的功能和效果。?

?

package javaplay.thread.test;import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue;public class BlockingQueueTest {public static void main(String[] args) {final BlockingQueue<Integer> queue = new ArrayBlockingQueue<>(3);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() + "準(zhǔn)備放數(shù)據(jù)!");queue.put(1);System.out.println(Thread.currentThread().getName() + "已經(jīng)放了數(shù)據(jù)," + "隊(duì)列目前有" + queue.size() + "個(gè)數(shù)據(jù)");} catch (InterruptedException e) {e.printStackTrace();}}}}.start();}new Thread() {public void run() {while (true) {try {// 將此處的睡眠時(shí)間分別改為100和1000,觀察運(yùn)行結(jié)果會(huì)發(fā)現(xiàn)經(jīng)常會(huì)有3個(gè)數(shù)據(jù) 100則因取得快很少有3Thread.sleep(1000);System.out.println(Thread.currentThread().getName() + "準(zhǔn)備取數(shù)據(jù)!");queue.take();System.out.println(Thread.currentThread().getName() + "已經(jīng)取走數(shù)據(jù)," + "隊(duì)列目前有" + queue.size() + "個(gè)數(shù)據(jù)");} catch (InterruptedException e) {e.printStackTrace();}}}}.start();} }

用兩個(gè)具有1個(gè)空間的隊(duì)列來實(shí)現(xiàn)同步通知的功能。

?

?

package javaplay.thread.test;import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue;public class BlockingQueueCommunication {public static void main(String[] args) {final Business business = new Business();new Thread(new Runnable() {@Overridepublic void run() {for (int i = 1; i <= 5; i++) {business.sub(i);}}}).start();for (int i = 1; i <= 5; i++) {business.main(i);}}static class Business {BlockingQueue<Integer> queue1 = new ArrayBlockingQueue<Integer>(1);BlockingQueue<Integer> queue2 = new ArrayBlockingQueue<Integer>(1);{// 此處不能加static(queue1/queue2都不是static) 這叫匿名構(gòu)造方法 運(yùn)行時(shí)機(jī)在任何構(gòu)造方法之前// Collections.synchronizedMap(null);try {System.out.println("xxxxxdfsdsafdsa");queue2.put(1);} catch (InterruptedException e) {e.printStackTrace();}}public void sub(int i) {try {queue1.put(1);} catch (InterruptedException e) {e.printStackTrace();}for (int j = 1; j <= 1; j++) {System.out.println("sub thread sequece of " + j + ",loop of " + i);}try {queue2.take();} catch (InterruptedException e) {e.printStackTrace();}}public void main(int i) {// 此處不能加synchronized 否則如果main先進(jìn)去就死鎖了try {queue2.put(1);} catch (InterruptedException e1) {e1.printStackTrace();}for (int j = 1; j <= 2; j++) {System.out.println("main thread sequece of " + j + ",loop of " + i);}try {queue1.take();} catch (InterruptedException e) {e.printStackTrace();}}}}

一個(gè)隊(duì)列可否實(shí)現(xiàn)呢?

?

轉(zhuǎn)載于:https://www.cnblogs.com/john8169/p/9780546.html

總結(jié)

以上是生活随笔為你收集整理的Java多线程之线程并发库阻塞队列的应用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。