線程池,顧名思義,放線程的池子嘛,這個池子可以存放多少線程取決於你自己采用什么樣的線程池,你的硬件資源,以及並發線程的數量。JDK提供了下面的四種線程池:
固定線程數的線程池
-
最簡單的
在Java中創建一個線程池,這很簡單,只需要兩行代碼。
ExecutorService executor = Executor.newFixedTreadPool(6);//固定線程是6
//線程一般設置成processor核心數的倍數,因為我這台機器是6核的,所以設成6。這也是充分利用硬件嘛
//執行線程任務
executor.execute(new Runnable(){
@Override
public void run(){
//do nothing
}
})
executor.shutdown();
Executor是Java並發包中提供的,用來創造不同類型的線程池。
Attention
但是在多人合作或者是一些部署上線的項目里,是不允許去使用這種方法的,因為它是有性能隱患的。
Executors在創建線程池的時候,用的是new LinkedBlockingQueue
它的問題就在於來者不拒,只要有任務來,你就進隊列等着。在入隊列和出隊列用的並不是同一個lock,在多processor的機器上,是可以做到真正意義上的並行的。拿經典的生產者和消費者來舉例子,在同一個時間點,有的在消費,有的在生產。
這種線程池不會銷毀線程,不會拒絕任務,固定線程數。所以如果不停的加入任務,會導致很糟糕的內存占用,老年代可能會被占滿。
-
稍復雜的(可以延時執行,也可以執行帶返回值的任務)
public static void main(String[] args) throws InterruptedException, ExecutionException {
TestThread testThread = new TestThread();
System.out.println(testThread.processors);
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(6);
FutureTask<String> futureTask = new FutureTask<>(new Callable<String>() {
@Override
public String call() throws Exception {
return Thread.currentThread().getName();
}
});
scheduledExecutorService.submit(futureTask);
//獲取返回值
String result = futureTask.get();
System.out.println("result :"+result);
//執行延時任務
scheduledExecutorService.schedule(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+": bomb!");
}
},3L,TimeUnit.SECONDS);
}
Output:
result :pool-1-thread-1
pool-1-thread-1: bomb!
緩存的線程池
核心池大小為0,線程池最大線程數目為最大整型,這意味着所有的任務一提交就會wait****。當線程池中的線程有60s沒有執行任務就會被Kill,阻塞隊列為SynchronousQueue。SynchronousQueue的take操作需要put操作等待,put操作需要take操作等待,否則會阻塞(線程池的阻塞隊列不能存儲,所以當目前線程處理忙碌狀態時,會開辟新的線程來處理請求**),線程進入wait set。
總結一下這是一個可以無限擴大的線程池;適合處理執行時間比較小的任務;線程空閑時間超過60s就會被Kill,所以長時間處於空閑狀態的時候,這種線程池幾乎不占用資源,因為它壓根沒有線程在里面;阻塞隊列沒有存儲空間,只要請求到來,就必須找到一條空閑線程去處理這個請求,找不到則在線程池新開辟一條線程。
如果主線程提交任務的速度遠遠大於CachedThreadPool的處理速度,則CachedThreadPool會不斷地創建新線程來執行任務,這樣有可能會導致系統耗盡CPU和內存資源,所以在使用該線程池時,要注意控制並發的任務數。如果是一個不斷增長的任務需求,很容易就會到性能瓶頸,它會不停的創建新的線程。
ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
for (int i = 0; i < 10; i++) {
cachedThreadPool.execute(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName());
}
});
}
cachedThreadPool.shutdown();
Output:
pool-1-thread-2
pool-1-thread-6
pool-1-thread-1
pool-1-thread-7
pool-1-thread-8
pool-1-thread-3
pool-1-thread-5
pool-1-thread-9
pool-1-thread-4
pool-1-thread-10
單個線程的線程池
SingleThreadExecutor 是使用單個worker線程的Executor。只有一種情況會有新的線程加入線程池,那就是原有的線程運行時有拋出異常,這時就會有創建的新的線程來替代它的工作。
拿生產者消費者模型來說的話,這就是一個單一消費者的模型。
(ps.一般可以用來做一些日志記錄
public static void main(String[] args) {
// 永遠是一條線程
ExecutorService singleThreadPool = Executors.newSingleThreadExecutor();
for (int i = 0; i < 10; i++) {
final int j = i;
singleThreadPool.execute(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + ":" + j);
}
});
}
singleThreadPool.shutdown();
}
Output:
pool-1-thread-1:0
pool-1-thread-1:1
pool-1-thread-1:2
pool-1-thread-1:3
pool-1-thread-1:4
pool-1-thread-1:5
pool-1-thread-1:6
pool-1-thread-1:7
pool-1-thread-1:8
pool-1-thread-1:9