線程池的原理正在學習中,如有不當之處,請勿見怪,這篇文章僅僅是我怕忘記而寫的。
1.線程池出現(xiàn)的原因
當需要使用很多個線程時,如果還是采用簡單的創(chuàng)建線程的方式,會導致系統(tǒng)開銷變大。
因為在創(chuàng)建線程和關閉線程中會占用系統(tǒng)開銷。如果一個線程在執(zhí)行完一個任務繼續(xù)執(zhí)行下一個任務,那么效率將大大提高。
2.線程池正是解決了上面的問題,只要定義好任務,然后交給線程池,你不需要知道它是如何執(zhí)行的、被哪個線程執(zhí)行的、什么時候執(zhí)行的。
3.具體的實現(xiàn)原理大家可以參照海子的博客:http://www.cnblogs.com/dolphin0520/p/3932921.html
4.簡單的線程池實例和分析
public class ExecutorCase {
private static Executor executor = Executors.newFixedThreadPool(5);
public static void main(String[] args) {
for (int i = 0; i <10 ; i++) {
System.out.println("第"+i+"次執(zhí)行");
executor.execute(new Task());
executor.execute(new Task1());
}
}
static class Task1 implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"---");
}
}
static class Task implements Runnable{
public void run(){
System.out.println(Thread.currentThread().getName());
}
}
}
執(zhí)行結(jié)果
第0次執(zhí)行
pool-1-thread-1
第1次執(zhí)行
第2次執(zhí)行
pool-1-thread-2---
pool-1-thread-3
pool-1-thread-4---
第3次執(zhí)行
pool-1-thread-1---
pool-1-thread-2---
pool-1-thread-3
第4次執(zhí)行
pool-1-thread-5
pool-1-thread-1---
pool-1-thread-4
第5次執(zhí)行
第6次執(zhí)行
pool-1-thread-3---
第7次執(zhí)行
pool-1-thread-5
pool-1-thread-2
pool-1-thread-3---
pool-1-thread-4
第8次執(zhí)行
pool-1-thread-1---
pool-1-thread-2---
pool-1-thread-5
第9次執(zhí)行
pool-1-thread-3
pool-1-thread-4---
5.結(jié)果分析
可以看到線程的執(zhí)行是沒有順序的,當executor.execute(new Task())執(zhí)行完成后就會創(chuàng)建一個線程,并執(zhí)行start()方法,讓線程處于就緒狀態(tài),至于什么時候它能執(zhí)行,這是由cpu調(diào)度決定的。
這里設置了corePoolSize為5,線程池的數(shù)量大于5后,新進來的任務將進入阻塞隊列中,等待線程池中的某個線程空閑下來后,再從阻塞隊列中獲取。
- 如果當前線程池中的線程數(shù)目小于corePoolSize,則每來一個任務,就會創(chuàng)建一個線程去執(zhí)行這個任務;
- 如果當前線程池中的線程數(shù)目>=corePoolSize,則每來一個任務,會嘗試將其添加到任務緩存隊列當中,若添加成功,則該任務會等待空閑線程將其取出去執(zhí)行;若添加失?。ㄒ话銇碚f是任務緩存隊列已滿),則會嘗試創(chuàng)建新的線程去執(zhí)行這個任務;
- 如果當前線程池中的線程數(shù)目達到maximumPoolSize,則會采取任務拒絕策略進行處理;
- 如果線程池中的線程數(shù)量大于 corePoolSize時,如果某線程空閑時間超過keepAliveTime,線程將被終止,直至線程池中的線程數(shù)目不大于corePoolSize;如果允許為核心池中的線程設置存活時間,那么核心池中的線程空閑時間超過keepAliveTime,線程也會被終止。