Java如何固定大小的線程池
線程池就是在程序啟動的時候先建立幾個可以使用的線程放在那里,然后等著具體的任務放進去,這個任務基本可以說都是Runnable的實現(xiàn)類,因此它減小了系統(tǒng)每次新建和銷毀線程的開銷,但同時增加了維護這些線程的開銷,個中取舍看具體情況而定。
固定大小的線程池就是在啟動的時候創(chuàng)建了固定個數(shù)的線程放在那里等待使用。
2.包裝一個線程池對象public class TaskPool{ private final ThreadPoolExecutor executor = (ThreadPoolExecutor)Executors.newFixedThreadPool(9); // 創(chuàng)建一個大小為9的固定線程池,可以按照CPU的核數(shù)初步判定,如果CPU密集性任務則創(chuàng)建N+1個,如果是IO密集型任務則創(chuàng)建2N+1個,其中N即CPU的核數(shù) protected void shutdown(){// do something// 這個方法等待線程池中所有已提交任務執(zhí)行結(jié)束,不接收新任務,然后結(jié)束executor.shutdown(); // 這個強制結(jié)束所有任務,然后正在等在的任務列表// executor.shutdownNow(); } protected void execute(Runnable command){// do something// 提交任務executor.execute(command); } public void status(){StringBuffer sb = new StringBuffer();// 當前正在執(zhí)行任務的線程數(shù)sb.append(executor.getActiveCount() + 'n'); // 當前正在等待執(zhí)行的線程數(shù)sb.append(executor.getQueue().size() + 'n'); // 返回已經(jīng)完成的線程數(shù)sb.append(executor.getCompletedTaskCount() + 'n'); System.out.println(sb.toString());// 注:以上方法都是返回一個大概值,因為線程在執(zhí)行中,這些狀態(tài)隨時都會改變 }} 3.使用線程池
public class Launcher{ private TaskPool taskPool = new TaskPool(); public static void main(String[] args){// 新建100個任務,Runnable的實現(xiàn)類TaskTask[] tasks = new Task[100];for (int i = 0; i < tasks.length; i++){ tasks[i] = new Task('Task ' + (i+1)); // 提交到線程池運行 taskPool.execute(task[i]); if ( i % 50 == 0){taskPool.status();} } private static class Task implements Runnable{private String name;public Task(String name){ this.name = name;}public void run(){ // do something System.out.println('我的名字是:' + this.name);} }}Java線程池小拓展線程池的介紹
1 常用的 池化技術(shù)
C3P0
DBCP
2 線程池的衍生
頻繁的創(chuàng)建線程對象和多線程之間進行上下文切換,是非常耗時間和資源的所以JDK1.5中提出了線程池技術(shù)
3 使用線程池
Exector
4 線程池的創(chuàng)建
創(chuàng)建一個固定大小的線程池 ( 最常用的方法 )
ExecutorService pool = Executors.newFixedThreadPool(2);Runnable task = new Runnable() {@Overridepublic void run() {while (true) {try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(Thread.currentThread().getName());}}};pool.execute(task);pool.execute(task);pool.execute(task);//線程池的帶下只有兩個 現(xiàn)在這個任務在其等待隊列中排隊等候
創(chuàng)建可變大小的線程池
ExecutorService pool = Executors.newCachedThreadPool();Runnable task = new Runnable() {@Overridepublic void run() {while (true) {try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(Thread.currentThread().getName());}}};pool.execute(task);pool.execute(task);pool.execute(task);
創(chuàng)建獨立任務的線程池
ExecutorService pool = Executors.newSingleThreadExecutor();Runnable task = new Runnable() {@Overridepublic void run() {while (true) {try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(Thread.currentThread().getName());}}};pool.execute(task);pool.execute(task);pool.execute(task);
創(chuàng)建可調(diào)度的線程池
ScheduledExecutorService threadPool = Executors.newScheduledThreadPool(2);Runnable task = new Runnable() {@Overridepublic void run() {while (true) {try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(Thread.currentThread().getName());}}};threadPool.schedule(task, 2000, TimeUnit.MILLISECONDS);
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 概述IE和SQL2k開發(fā)一個XML聊天程序2. ASP刪除img標簽的style屬性只保留src的正則函數(shù)3. 讀大數(shù)據(jù)量的XML文件的讀取問題4. ASP基礎(chǔ)入門第八篇(ASP內(nèi)建對象Application和Session)5. ASP常用日期格式化函數(shù) FormatDate()6. jsp+servlet簡單實現(xiàn)上傳文件功能(保存目錄改進)7. 解析原生JS getComputedStyle8. jsp實現(xiàn)登錄驗證的過濾器9. msxml3.dll 錯誤 800c0019 系統(tǒng)錯誤:-2146697191解決方法10. ASP基礎(chǔ)知識Command對象講解
