日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

线程池ThreadPoolExcutor

發布時間:2025/3/20 编程问答 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 线程池ThreadPoolExcutor 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.



/*
* interface Executor 執行行程接口
* +executor(Runnable object)執行任務
* interface ExecutorService 管理線程接口
* +shutdown()關閉執行器
* +shutdownNow()立即關閉執行器,返回未完成任務清單
* +isShutdown執行器已關閉返回true
* +isTerminated想線程池中所有任務都被終結,返回true
*
* Executors類的靜態方法
* +newFixedThreadPool(numberOfThreads:int):ExecutorService 創建一個固定線程數量的線程池,并行執行的線程數量不變,線程當前任務完成后,可以被重用執行另一個任務
* +newCachedThreadPool():ExecutorService 創建一個線程池,按需創建新線程,但當前面創建的線程可用時,則重用它們
*/

import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class ThreadPoolExcutorDemo {public static void main(String[] args) {// TODO Auto-generated method stub/** 當n=1時線程1,2,3順序執行* 當n=2時線程1,2同時執行,線程3等待,當有線程空閑時,執行線程3* 當n3時線程1,2,3同時執行*/ExecutorService executor=Executors.newFixedThreadPool(3);//創建一個固定3個線程的線程池executor.execute(new PrintChar('a',50));//執行線程1executor.execute(new PrintNum(50));//執行線程2executor.execute(new PrintChar('b',50));//執行線程3executor.shutdown();//關閉執行器,不在接受新任務}/** interface Executor 執行行程接口* +executor(Runnable object)執行任務* interface ExecutorService 管理線程接口* +shutdown()關閉執行器* +shutdownNow()立即關閉執行器,返回未完成任務清單* +isShutdown執行器已關閉返回true* +isTerminated想線程池中所有任務都被終結,返回true* * Executors類的靜態方法* +newFixedThreadPool(numberOfThreads:int):ExecutorService 創建一個固定線程數量的線程池,并行執行的線程數量不變,線程當前任務完成后,可以被重用執行另一個任務* +newCachedThreadPool():ExecutorService 創建一個線程池,按需創建新線程,但當前面創建的線程可用時,則重用它們*///任務類public static class PrintChar implements Runnable{private char charToPrint;private int times;PrintChar(char c,int t){charToPrint=c;times=t;}@Overridepublic void run() {// TODO Auto-generated method stubfor(int i=0;i<times;i++){System.out.print(charToPrint);}} }//任務類public static class PrintNum implements Runnable{private int lastNum;PrintNum(int n){lastNum=n;}@Overridepublic void run() {// TODO Auto-generated method stubfor(int i=0;i<lastNum;i++){System.out.print(" "+i);} } } }

總結

以上是生活随笔為你收集整理的线程池ThreadPoolExcutor的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。