国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

線程池管理類

2019-11-09 17:17:30
字體:
供稿:網(wǎng)友
/** * 線程管理器 * */public class ThreadManager { PRivate static ThreadPool mThreadPool; public static ThreadPool getThreadPool() { if (mThreadPool == null) { synchronized (ThreadManager.class) { if (mThreadPool == null) { int cpuCount = Runtime.getRuntime().availableProcessors();// 獲取cpu數(shù)量 System.out.println("cup個數(shù):" + cpuCount); int threadCount = cpuCount * 2 + 1;//線程個數(shù) mThreadPool = new ThreadPool(threadCount, threadCount, 1L); } } } return mThreadPool; } // 線程池 public static class ThreadPool { private int corePoolSize;// 核心線程數(shù) private int maximumPoolSize;// 最大線程數(shù) private long keepAliveTime;// 休息時間 private ThreadPoolExecutor executor; private ThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime) { this.corePoolSize = corePoolSize; this.maximumPoolSize = maximumPoolSize; this.keepAliveTime = keepAliveTime; } // 線程池幾個參數(shù)的理解: // 比如去火車站買票, 有10個售票窗口, 但只有5個窗口對外開放. 那么對外開放的5個窗口稱為核心線程數(shù), // 而最大線程數(shù)是10個窗口. // 如果5個窗口都被占用, 那么后來的人就必須在后面排隊, 但后來售票廳人越來越多, 已經(jīng)人滿為患, 就類似于線程隊列已滿. // 這時候火車站站長下令, 把剩下的5個窗口也打開, 也就是目前已經(jīng)有10個窗口同時運行. 后來又來了一批人, // 10個窗口也處理不過來了, 而且售票廳人已經(jīng)滿了, 這時候站長就下令封鎖入口,不允許其他人再進來, 這就是線程異常處理策略. // 而線程存活時間指的是, 允許售票員休息的最長時間, 以此限制售票員偷懶的行為. public void execute(Runnable r) { if (executor == null) { executor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), Executors.defaultThreadFactory(), new AbortPolicy()); // 參1:核心線程數(shù);參2:最大線程數(shù);參3:線程休眠時間;參4:時間單位;參5:線程隊列;參6:生產(chǎn)線程的工廠;參7:線程異常處理策略 } // 線程池執(zhí)行一個Runnable對象, 具體運行時機線程池說了算 executor.execute(r); } // 取消任務(wù) public void cancel(Runnable r) { if (executor != null) { // 從線程隊列中移除對象 executor.getQueue().remove(r); } } }}

調(diào)用:ThreadManager.getThreadPool.execute(new runnable(){ //自己要執(zhí)行的代碼 });


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 原平市| 镇雄县| 商城县| 永和县| 龙游县| 贺兰县| 红原县| 平山县| 丰宁| 修文县| 迁安市| 马龙县| 读书| 吴旗县| 花莲市| 正镶白旗| 竹北市| 青田县| 自贡市| 平遥县| 寿宁县| 九龙城区| 天柱县| 洛宁县| 信宜市| 临夏市| 巍山| 师宗县| 腾冲县| 电白县| 阿拉善左旗| 太康县| 蓬莱市| 紫金县| 凉山| 抚松县| 英吉沙县| 都昌县| 出国| 玉屏| 扎兰屯市|