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

歡迎訪問 生活随笔!

生活随笔

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

java

java 线程包_Java 多线程——工具包

發(fā)布時間:2025/4/5 java 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 线程包_Java 多线程——工具包 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

JUC

首先來說說synchronized的有什么缺點(diǎn)。

它非常的死板。要么獲取鎖,要么等待鎖,而且我們無法知曉此時這個鎖有沒有被人拿到,也不知道某個線程是否是上鎖狀態(tài)。

只有悲觀鎖、排他鎖,沒有樂觀鎖、共享鎖。有些資源允許很多線程去讀,但是只允許一個線程寫,這樣的鎖叫做共享鎖。悲觀鎖就是做某個操作必須要拿到一個鎖。樂觀鎖就是先去把一個任務(wù)做完,但由于多個線程競爭這個操作沒有正確完成,那就重新做一遍,直到正確為止。

性能相對較差

java.util.concurrent簡稱JUC,是Java的并發(fā)工具包,解決了上述的一些問題。

Lock / Condition

Lock提供了一個更加復(fù)雜的鎖機(jī)制。它允許更加靈活的結(jié)構(gòu),支持不同的方法,支持相關(guān)的Condition操作。

同一個鎖可以有多個條件。

讀寫分離。Lock的實(shí)現(xiàn)中有讀鎖WriteLock和寫鎖ReadLock。

tryLock方法。如果該鎖可用立刻返回true,不可用則立刻返回false。

可以方便的實(shí)現(xiàn)更加靈活的優(yōu)先級/公平性。公平性指的是,假如有多個線程競爭一個鎖的話,我以什么順序給它們。非公平鎖,主要靠操作系統(tǒng)的調(diào)度。Lock的實(shí)現(xiàn)中有可重入鎖ReentrantLock(默認(rèn)是非公平鎖,可以傳一個參數(shù)讓它變成公平鎖)。

Conditon把Object的monitor方法(wait,notify,notifyAll)拆出來成了獨(dú)立的對象,使得支持多個等待集合。

CountDownLatch

倒數(shù)閉鎖。

用于協(xié)調(diào)一組線程的工作。

它的API簡單暴力countDown(),await()。以下是一個使用例子:

public class Main {

public static void main(String[] args) throws InterruptedException {

final CountDownLatch countDownLatch = new CountDownLatch(10);

for (int i = 0; i < 10; i++) {

int finalI = i;

new Thread(() -> {

int second = new Random().nextInt(10);

try {

Thread.sleep(second * 1000);

} catch (InterruptedException e) {

e.printStackTrace();

}

System.out.println("線程" + finalI + "干完活了");

countDownLatch.countDown();

}).start();

}

countDownLatch.await();

System.out.println("老板發(fā)話了,所有人干完活了!");

}

}

CyclicBarrier

循環(huán)的屏障

等所有線程執(zhí)行完了,才繼續(xù):

public class Main {

public static void main(String[] args) throws InterruptedException {

final CyclicBarrier cyclicBarrier = new CyclicBarrier(5);

for (int i = 0; i < 5; i++) {

int finalI = i;

new Thread(() -> {

int second = new Random().nextInt(10);

try {

Thread.sleep(second * 1000);

} catch (InterruptedException e) {

e.printStackTrace();

}

System.out.println("線程" + finalI + "干完活了");

try {

cyclicBarrier.await();

} catch (InterruptedException | BrokenBarrierException e) {

e.printStackTrace();

}

System.out.println("所有線程都執(zhí)行完了,大家一起說!");

}).start();

}

}

}

Semaphore

信號量

信號量的獲取和釋放

BlockingQueue & BlockingDeque

傳統(tǒng)的集合框架的操作要么正常返回,要么丟出異常,BlockingQueue/BlockingDeque提供?種「等待」的可能。

API: 阻塞操作:put/take。

先聲明容量大小,如果put超過容量,則會等待另一個線程take。

BlockingQueue先進(jìn)先出。BlockingDeque頭尾都能進(jìn)出,更加靈活。

Future & ExecutorService

Future代表?個「未來才會發(fā)?的事情」。

Future本身是?即返回的。

get()會阻塞并返回執(zhí)?結(jié)果,并拋出可能的異常。異常的拋出一般只會在當(dāng)前線程,而get可以把其他線程的異常轉(zhuǎn)移到當(dāng)前線程拋出。

線程池的參數(shù)。建議看ExecutorService的一個實(shí)現(xiàn):ThreadPoolExecutor文檔。

* Creates a new {@code ThreadPoolExecutor} with the given initial

* parameters and default rejected execution handler.

*

-----* 核心線程的數(shù)量,哪怕它們閑下來,也不會被丟棄。

* @param corePoolSize the number of threads to keep in the pool, even

* if they are idle, unless {@code allowCoreThreadTimeOut} is set

-----* 最多可以開這么多的線程工作。

* @param maximumPoolSize the maximum number of threads to allow in the

* poo

-----* 下面兩個是聯(lián)合使用的,非核心線程空閑多久會被殺死。

* @param keepAliveTime when the number of threads is greater than

* the core, this is the maximum time that excess idle threads

* will wait for new tasks before terminating.

* @param unit the time unit for the {@code keepAliveTime} argument

-----* 保存那些還沒被執(zhí)行的任務(wù)。

* @param workQueue the queue to use for holding tasks before they are

* executed. This queue will hold only the {@code Runnable}

* tasks submitted by the {@code execute} method.

-----* 每當(dāng)你需要一個新的線程的時候,用它來創(chuàng)建。

* @param threadFactory the factory to use when the executor

* creates a new thread

-----* 假如任務(wù)來的太快,把任務(wù)隊(duì)列撐滿了,要采取何種策略?

-----* AbortPolicy丟棄策略、CallerRunsPolicy讓調(diào)用者執(zhí)行

-----* DiscardOldestPolicy丟棄最老的一個任務(wù)、DiscardOldestPolicy丟棄最新的一個任務(wù)。

* @param handler the handler to use when execution is blocked

* because the thread bounds and queue capacities are reached

還可以舉一個通俗易懂的例子,假如你是一個老板,有如下的參數(shù)解釋:

corePoolSize 核?員?數(shù)量

maximumPoolSize 最?招募的員?數(shù)量

keepAliveTime/unit 員?閑下來多久之后炒掉他們

workQueue 訂單隊(duì)列

threadFactory 造?的??

handler 訂單實(shí)在太多的處理策略

Java 默認(rèn)實(shí)現(xiàn)的線程池,使用Executors.xxx創(chuàng)建一個線程池,且這些方法大多都返回一個ThreadPoolExecutor,如下所示:

public static ExecutorService newxxxThreadPool(xxx) {

return new ThreadPoolExecutor(六個參數(shù));

}

總結(jié)

以上是生活随笔為你收集整理的java 线程包_Java 多线程——工具包的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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