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

目前創(chuàng)新互聯(lián)已為近千家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)絡(luò)空間、網(wǎng)站改版維護(hù)、企業(yè)網(wǎng)站設(shè)計(jì)、瑪納斯網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。
1. 使用 Executors 工廠方法
Executors 類提供了一些工廠方法來創(chuàng)建不同類型的線程池,下面是一些常用的方法:
固定大小的線程池: 使用 Executors.newFixedThreadPool(int nThreads) 創(chuàng)建一個(gè)固定大小的線程池。
單線程線程池: 使用 Executors.newSingleThreadExecutor() 創(chuàng)建一個(gè)只有一個(gè)工作線程的線程池。
緩存線程池: 使用 Executors.newCachedThreadPool() 創(chuàng)建一個(gè)可緩存的線程池,如果線程空閑,則會(huì)被回收。
調(diào)度線程池: 使用 Executors.newScheduledThreadPool(int corePoolSize) 創(chuàng)建一個(gè)可以執(zhí)行延遲任務(wù)或周期性任務(wù)的線程池。
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)建一個(gè)線程池,這允許你更細(xì)粒度地控制線程池的行為。
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, // 空閑線程存活時(shí)間
TimeUnit.SECONDS, // 空閑線程存活時(shí)間的單位
new LinkedBlockingQueue(), // 工作隊(duì)列
new ThreadFactoryBuilder().setNameFormat("threadpool%d").build() // 線程工廠
);
}
}
3. 使用第三方庫
有些第三方庫也提供了創(chuàng)建線程池的功能,Google Guava 和 Apache Commons Pool,這些庫通常提供了更多的配置選項(xiàng)和額外的功能。
// 使用 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. 使用自定義實(shí)現(xiàn)
你也可以自己實(shí)現(xiàn)一個(gè)線程池,雖然 Java 標(biāo)準(zhǔn)庫已經(jīng)提供了非常完善的線程池實(shí)現(xiàn),但在某些特殊情況下,你可能需要自己實(shí)現(xiàn)一個(gè)線程池來滿足特定的需求。
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();
}
}
}
}
}
網(wǎng)站欄目:創(chuàng)建線程池的幾種方式
本文網(wǎng)址:http://www.dlmjj.cn/article/dpeppcg.html


咨詢
建站咨詢
