新聞中心
創(chuàng)建線程池的幾種方式

目前創(chuàng)新互聯(lián)已為近千家的企業(yè)提供了網(wǎng)站建設、域名、網(wǎng)絡空間、網(wǎng)站改版維護、企業(yè)網(wǎng)站設計、瑪納斯網(wǎng)站維護等服務,公司將堅持客戶導向、應用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
1. 使用 Executors 工廠方法
Executors 類提供了一些工廠方法來創(chuàng)建不同類型的線程池,下面是一些常用的方法:
固定大小的線程池: 使用 Executors.newFixedThreadPool(int nThreads) 創(chuàng)建一個固定大小的線程池。
單線程線程池: 使用 Executors.newSingleThreadExecutor() 創(chuàng)建一個只有一個工作線程的線程池。
緩存線程池: 使用 Executors.newCachedThreadPool() 創(chuàng)建一個可緩存的線程池,如果線程空閑,則會被回收。
調(diào)度線程池: 使用 Executors.newScheduledThreadPool(int corePoolSize) 創(chuàng)建一個可以執(zhí)行延遲任務或周期性任務的線程池。
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Main {
public static void main(String[] args) {
// 創(chuàng)建固定大小的線程池
ExecutorService fixedThreadPool = Executors.newFixedThreadPool(5);
// 創(chuàng)建單線程線程池
ExecutorService singleThreadPool = Executors.newSingleThreadExecutor();
// 創(chuàng)建緩存線程池
ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
// 創(chuàng)建調(diào)度線程池
ExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(2);
}
}
2. 使用 ThreadPoolExecutor 構(gòu)造函數(shù)
你也可以通過調(diào)用 ThreadPoolExecutor 的構(gòu)造函數(shù)直接創(chuàng)建一個線程池,這允許你更細粒度地控制線程池的行為。
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class Main {
public static void main(String[] args) {
// 創(chuàng)建線程池
ThreadPoolExecutor threadPool = new ThreadPoolExecutor(
2, // 核心線程數(shù)
4, // 最大線程數(shù)
60L, // 空閑線程存活時間
TimeUnit.SECONDS, // 空閑線程存活時間的單位
new LinkedBlockingQueue(), // 工作隊列
new ThreadFactoryBuilder().setNameFormat("threadpool%d").build() // 線程工廠
);
}
}
3. 使用第三方庫
有些第三方庫也提供了創(chuàng)建線程池的功能,Google Guava 和 Apache Commons Pool,這些庫通常提供了更多的配置選項和額外的功能。
// 使用 Google Guava 創(chuàng)建線程池
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.google.common.util.concurrent.MoreExecutors;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Main {
public static void main(String[] args) {
// 創(chuàng)建線程池
ExecutorService threadPool = MoreExecutors.newDirectExecutorService();
}
}
4. 使用自定義實現(xiàn)
你也可以自己實現(xiàn)一個線程池,雖然 Java 標準庫已經(jīng)提供了非常完善的線程池實現(xiàn),但在某些特殊情況下,你可能需要自己實現(xiàn)一個線程池來滿足特定的需求。
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;
public class CustomThreadPool {
private final AtomicInteger threadNumber = new AtomicInteger(1);
private final LinkedTransferQueue taskQueue = new LinkedTransferQueue<>();
private final WorkerThread[] threads;
public CustomThreadPool(int numberOfThreads) {
threads = new WorkerThread[numberOfThreads];
for (int i = 0; i < numberOfThreads; i++) {
threads[i] = new WorkerThread("CustomThreadPool" + threadNumber.getAndIncrement());
threads[i].start();
}
}
public void execute(Runnable task) {
taskQueue.offer(task);
}
private class WorkerThread extends Thread {
public WorkerThread(String name) {
super(name);
}
@Override
public void run() {
Runnable task;
while (true) {
try {
task = taskQueue.take();
task.run();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
文章標題:創(chuàng)建線程池的幾種方式
分享路徑:http://m.fisionsoft.com.cn/article/dpeppcg.html


咨詢
建站咨詢
