這篇文章主要為大家展示了“Java如何實(shí)現(xiàn)手寫(xiě)線程池”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“Java如何實(shí)現(xiàn)手寫(xiě)線程池”這篇文章吧。
目前創(chuàng)新互聯(lián)已為超過(guò)千家的企業(yè)提供了網(wǎng)站建設(shè)、域名、雅安服務(wù)器托管、網(wǎng)站托管、企業(yè)網(wǎng)站設(shè)計(jì)、盤州網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。
具體內(nèi)容如下
1.線程池是一種多線程處理形式,處理過(guò)程中將任務(wù)添加到隊(duì)列,然后在創(chuàng)建線程后自動(dòng)啟動(dòng)這些任務(wù)。線程池線程都是后臺(tái)線程。
2.線程池簡(jiǎn)易架構(gòu)
3.簡(jiǎn)易線程池代碼(自行優(yōu)化)
import java.util.List; /** * 線程接口 * * @Author yjian * @Date 14:49 2017/10/14 **/ public interface IThreadPool { //加入任務(wù) void execute(Runnable task); //加入任務(wù) void execute(Runnable[] tasks); //加入任務(wù) void execute(List<Runnable> tasks); //銷毀線程 void destroy(); }
import java.util.LinkedList; import java.util.List; import java.util.concurrent.atomic.AtomicLong; /** * 線程實(shí)現(xiàn)類(簡(jiǎn)易實(shí)現(xiàn),自行優(yōu)化.提供思路) * * @Author yjian * @Date 14:49 2017/10/14 **/ @SuppressWarnings("ALL") public class ThreadPoolImpl implements IThreadPool { //默認(rèn)開(kāi)啟線程個(gè)數(shù) static int WORKER_NUMBER = 5; //完成任務(wù)線程數(shù) 可見(jiàn)性 static volatile int sumCount = 0; //任務(wù)隊(duì)列 list非線程安全,可以優(yōu)化為BlockingQueue static List<Runnable> taskQueue = new LinkedList<Runnable>(); //線程工作組 WorkerThread[] workThreads; //原子性 static AtomicLong threadNum = new AtomicLong(); static ThreadPoolImpl threadPool; //構(gòu)造方法 public ThreadPoolImpl() { this(WORKER_NUMBER); } public ThreadPoolImpl(int workerNum) { this.WORKER_NUMBER = workerNum; //開(kāi)辟工作線程空間 workThreads = new WorkerThread[WORKER_NUMBER]; //開(kāi)始創(chuàng)建工作線程 for (int i = 0; i < WORKER_NUMBER; i++) { workThreads[i] = new WorkerThread(); Thread thread = new Thread(workThreads[i], "ThreadPool-worker" + threadNum.incrementAndGet()); System.out.println("初始化線程數(shù)" + (i + 1) + "---------當(dāng)前線程名稱:" + thread.getName()); thread.start(); } } @Override public String toString() { return "工作線程數(shù)量為" + WORKER_NUMBER + "已完成的任務(wù)數(shù)" + sumCount + "等待任務(wù)數(shù)量" + taskQueue.size(); } //獲取線程池 public static IThreadPool getThreadPool() { return getThreadPool(WORKER_NUMBER); } public static IThreadPool getThreadPool(int workerNum) { //容錯(cuò)性,如果小于等于0就默認(rèn)線程數(shù) if (workerNum <= 0) { workerNum = WORKER_NUMBER; } if (threadPool == null) { threadPool = new ThreadPoolImpl(workerNum); } return threadPool; } @Override public void execute(Runnable task) { synchronized (taskQueue) { taskQueue.add(task); taskQueue.notifyAll(); } } @Override public void execute(Runnable[] tasks) { synchronized (taskQueue) { for (Runnable task : tasks) { taskQueue.add(task); } taskQueue.notifyAll(); } } @Override public void execute(List<Runnable> tasks) { synchronized (taskQueue) { for (Runnable task : tasks) { taskQueue.add(task); } taskQueue.notifyAll(); } } @Override public void destroy() { //循環(huán)是否還存在任務(wù),如果存在等待20毫秒處理時(shí)間 while (!taskQueue.isEmpty()) { try { Thread.sleep(20); } catch (InterruptedException e) { e.printStackTrace(); } } //如果任務(wù)隊(duì)列已處理完成,銷毀線程,清空任務(wù) for (int i = 0; i < WORKER_NUMBER; i++) { workThreads[i].setWorkerFlag(); workThreads[i] = null; } threadPool = null; taskQueue.clear(); } //創(chuàng)建工作線程池 class WorkerThread extends Thread { //用來(lái)標(biāo)識(shí)當(dāng)前線程屬于活動(dòng)可用狀態(tài) private boolean isRunning = true; @Override public void run() { Runnable runnable = null; //死循環(huán) while (isRunning) { //非線程安全,所以采用同步鎖 synchronized (taskQueue) { while (isRunning && taskQueue.isEmpty()) { try { //如果任務(wù)隊(duì)列為空,等待20毫秒 監(jiān)聽(tīng)任務(wù)到達(dá) taskQueue.wait(20); } catch (Exception e) { e.printStackTrace(); } } //任務(wù)隊(duì)列不為空 if (!taskQueue.isEmpty()) { runnable = taskQueue.remove(0);//獲取第一個(gè)任務(wù) } } if (runnable != null) { runnable.run(); } sumCount++; runnable = null; } } //銷毀線程 public void setWorkerFlag() { isRunning = false; } } }
import java.util.ArrayList; import java.util.List; /** * 測(cè)試類 * * @Author yjian * @Date 15:37 2017/10/14 **/ public class ThreadPoolTest { public static void main(String[] args) { //獲取線程池 IThreadPool t = ThreadPoolImpl.getThreadPool(20); List<Runnable> taskList = new ArrayList<Runnable>(); for (int i = 0; i < 100; i++) { taskList.add(new Task()); } //執(zhí)行任務(wù) t.execute(taskList); System.out.println(t); //銷毀線程 t.destroy(); System.out.println(t); } static class Task implements Runnable { private static volatile int i = 1; @Override public void run() { System.out.println("當(dāng)前處理的線程:" + Thread.currentThread().getName() + " 執(zhí)行任務(wù)" + (i++) + " 完成"); } } }
對(duì)spring源碼研究的,仔細(xì)查看代碼用了哪幾種spring常用的模式。寫(xiě)程序的規(guī)范應(yīng)該和spring一樣。
以上是“Java如何實(shí)現(xiàn)手寫(xiě)線程池”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
當(dāng)前標(biāo)題:Java如何實(shí)現(xiàn)手寫(xiě)線程池
文章轉(zhuǎn)載:http://muchs.cn/article22/gecjcc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航、定制網(wǎng)站、面包屑導(dǎo)航、電子商務(wù)、微信公眾號(hào)、網(wǎng)站內(nèi)鏈
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)