本文實例為大家分享了Java手寫線程池的實現代碼,供大家參考,具體內容如下
1.線程池是一種多線程處理形式,處理過程中將任務添加到隊列,然後在創建線程後自動啟動這些任務。線程池線程都是後台線程。
2.線程池簡易架構
3.簡易線程池代碼(自行優化)
import java.util.List;/** * 線程接口* * @Author yjian * @Date 14:49 2017/10/14 **/public interface IThreadPool { //加入任務void execute(Runnable task); //加入任務void execute(Runnable[] tasks); //加入任務void execute(List<Runnable> tasks); //銷毀線程void destroy();} import java.util.LinkedList;import java.util.List;import java.util.concurrent.atomic.AtomicLong;/** * 線程實現類(簡易實現,自行優化.提供思路) * * @Author yjian * @Date 14:49 2017/10/14 **/@SuppressWarnings("ALL")public class ThreadPoolImpl implements IThreadPool { //默認開啟線程個數static int WORKER_NUMBER = 5; //完成任務線程數可見性static volatile int sumCount = 0; //任務隊列list非線程安全,可以優化為BlockingQueue static List<Runnable> taskQueue = new LinkedList<Runnable>(); //線程工作組WorkerThread[] workThreads; //原子性static AtomicLong threadNum = new AtomicLong(); static ThreadPoolImpl threadPool; //構造方法public ThreadPoolImpl() { this(WORKER_NUMBER); } public ThreadPoolImpl(int workerNum) { this.WORKER_NUMBER = workerNum; //開闢工作線程空間workThreads = new WorkerThread[WORKER_NUMBER]; //開始創建工作線程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("初始化線程數" + (i + 1) + "---------當前線程名稱:" + thread.getName()); thread.start(); } } @Override public String toString() { return "工作線程數量為" + WORKER_NUMBER + "已完成的任務數" + sumCount + "等待任務數量" + taskQueue.size(); } //獲取線程池public static IThreadPool getThreadPool() { return getThreadPool(WORKER_NUMBER); } public static IThreadPool getThreadPool(int workerNum) { //容錯性,如果小於等於0就默認線程數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() { //循環是否還存在任務,如果存在等待20毫秒處理時間while (!taskQueue.isEmpty()) { try { Thread.sleep(20); } catch (InterruptedException e) { e.printStackTrace(); } } //如果任務隊列已處理完成,銷毀線程,清空任務for (int i = 0; i < WORKER_NUMBER; i++) { workThreads[i].setWorkerFlag(); workThreads[i] = null; } threadPool = null; taskQueue.clear(); } //創建工作線程池class WorkerThread extends Thread { //用來標識當前線程屬於活動可用狀態private boolean isRunning = true; @Override public void run() { Runnable runnable = null; //死循環while (isRunning) { //非線程安全,所以採用同步鎖synchronized (taskQueue) { while (isRunning && taskQueue.isEmpty()) { try { //如果任務隊列為空,等待20毫秒監聽任務到達taskQueue.wait(20); } catch (Exception e) { e.printStackTrace(); } } //任務隊列不為空if (!taskQueue.isEmpty()) { runnable = taskQueue.remove(0);//獲取第一個任務} } if (runnable != null) { runnable.run(); } sumCount++; runnable = null; } } //銷毀線程public void setWorkerFlag() { isRunning = false; } }} import java.util.ArrayList;import java.util.List;/** * 測試類* * @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()); } //執行任務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("當前處理的線程:" + Thread.currentThread().getName() + " 執行任務" + (i++) + " 完成"); } }}對spring源碼研究的,仔細查看代碼用了哪幾種spring常用的模式。寫程序的規範應該和spring一樣。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。