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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java笔记-concurrent集合及线程池

發(fā)布時間:2025/3/15 java 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java笔记-concurrent集合及线程池 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

reentrantLock + condition實現(xiàn) Blocking queue。

java.util.concurrent提供了線程安全的Blocking集合:ArrayBlockingQueue

程序運行截圖如下:

源碼如下:

import java.lang.reflect.Array; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue;class WorkerThread extends Thread{BlockingQueue<String> taskQueue;public WorkerThread(BlockingQueue<String> taskQueue){this.taskQueue = taskQueue;}@Overridepublic void run() {while(!isInterrupted()){String name = "";try{name = taskQueue.take();}catch (InterruptedException e){break;}String result = "Hello " + name + " !";System.out.println(result);}} }public class Main1 {public static void main(String[] args) throws InterruptedException {BlockingQueue<String> taskQueue = new ArrayBlockingQueue<String>(1000);WorkerThread workerThread = new WorkerThread(taskQueue);workerThread.start();taskQueue.put("小紅");Thread.sleep(1000);taskQueue.put("小剛");Thread.sleep(1000);taskQueue.put("小明");Thread.sleep(1000);workerThread.interrupt();workerThread.join();System.out.println("END");} }
InterfaceNon-thread safeThread safe
ListArrayListCopyOnWriteArrayList
MapHashMapConcurrentHashMap
SetHashSet, TreeSetCopyOnWriteArraySet
QueueArrayDeque, LinkedListArrayBlockingQueue, LinkedBlockingQueue
DequeArrayDequeue, LinkedList

LinkedBlockingDeque

java.util.Collections工具類還提供了舊的線程安全集合轉換器:

Map unsafeMap = new HashMap(); Map threadSafeMap = Collections.synchronizedMap(unsafeMap);

使用java.util.concurrent提供的Blocking集合可以簡化多線程編程:

多線程同時訪問Blocking集合是安全的。

盡可能使用JDK提供的concurrent集合,避免自己編寫同步代碼。

?

Atomic

java.util.concurrent.atomic提供了一組原子類型操作。

Atomic類可以實現(xiàn):

無鎖(lock-free)實現(xiàn)的線程安全(thread-safe)訪問

程序運行截圖如下:

代碼如下:

import java.util.concurrent.atomic.AtomicInteger;class Counter{private AtomicInteger value = new AtomicInteger(0);public int add(int m){//System.out.println("add: " + value.get());return this.value.addAndGet(m);}public int dec(int m){//System.out.println("dec: " + value.get());return this.value.addAndGet(-m);}public int get(){return this.value.get();} } public class Main2 {static final private int LOOP = 1000;public static void main(String[] args) throws InterruptedException {final Counter counter = new Counter();Thread t1 = new Thread(){@Overridepublic void run() {for(int i = 0; i < LOOP; i++){counter.add(1);}}};Thread t2 = new Thread(){@Overridepublic void run() {for(int i = 0; i < LOOP; i++){counter.dec(1);}}};t1.start();t2.start();t1.join();t2.join();System.out.println("COUNTER:" + counter.get());} }

使用java.util.atomic提供的原子操作可以簡化多線程編程:

AtomicInteger/AtomicLong/AtomicIntegerArray。

原子操作實現(xiàn)了無鎖的線程安全。

適用于計數(shù)器,累加器。

?

ExecutorService

Java語言內置多線程支持:

創(chuàng)建線程需要操作系統(tǒng)資源(線程資源,棧空間...)

頻繁串講和消耗線程需要大量事件。

?

線程池:

線程池維護若干個線程,處于等待狀態(tài)。

如果有新任務,就分配一個空閑線程執(zhí)行。

如果所有線程都處于忙碌狀態(tài),新任務放入隊列等待

?

程序運行截圖如下:

源碼如下:

import java.util.concurrent.*;class PrintTask implements Runnable{String name;public PrintTask(String name){this.name = name;}public void run() {for(int i = 0; i < 3; i++){System.out.println("Hello " + name + " !");try{Thread.sleep(1000);}catch (InterruptedException e){}}} }public class Main3 {public static void main(String[] args) throws InterruptedException {//ExecutorService executor = Executors.newFixedThreadPool(3);//ExecutorService executor = Executors.newSingleThreadExecutor();//ExecutorService executor = Executors.newCachedThreadPool();ExecutorService executor = new ThreadPoolExecutor(0, 10, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>());executor.submit(new PrintTask("小明"));executor.submit(new PrintTask("小紅"));executor.submit(new PrintTask("小剛"));executor.submit(new PrintTask("小粉"));Thread.sleep(10000);executor.shutdown();} }

?

常用ExecutorService:

FixedThreadPool:線程數(shù)固定

CachedThreadPool:線程數(shù)根據任務動態(tài)調整

SingleThreadExecutor:僅單線程執(zhí)行

?

多線程?

ScheduledThreadPool:一個任務可以定期反復執(zhí)行

程序運行截圖如下;

源碼如下:

import java.text.SimpleDateFormat; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit;class HelloTask implements Runnable{String name;public HelloTask(String name){this.name = name;}public void run() {System.out.println("Hello " + name + " !");try{Thread.sleep(1000);}catch (InterruptedException e){}SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");System.out.println("GOODBYE " + name + " " + df.format(System.currentTimeMillis()));} }public class Main4 {public static void main(String[] args){ScheduledExecutorService executor = Executors.newScheduledThreadPool(3);executor.scheduleAtFixedRate(new HelloTask("小明"), 2, 5, TimeUnit.SECONDS);executor.scheduleAtFixedRate(new HelloTask("小紅"), 2, 5, TimeUnit.SECONDS);} }

?

總結

以上是生活随笔為你收集整理的Java笔记-concurrent集合及线程池的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。