您好,登錄后才能下訂單哦!
這篇“Java中線程池自定義如何實現”文章的知識點大部分人都不太理解,所以小編給大家總結了以下內容,內容詳細,步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“Java中線程池自定義如何實現”文章吧。
從源碼可以得知,調用start方法時,程序還會判斷當前的線程狀態
這里又引申出另一個問題,線程到底有幾種狀態
年輕的時候背八股文時,只是說五種狀態,這五種狀態也不知道是哪里來的,不知道有沒有人和我一樣,當初只是知其然不知其所以然。貼出源碼來:
public enum State { /** * Thread state for a thread which has not yet started. */ NEW, // 新建 /** * Thread state for a runnable thread. A thread in the runnable * state is executing in the Java virtual machine but it may * be waiting for other resources from the operating system * such as processor. */ RUNNABLE, // 運行中 /** * Thread state for a thread blocked waiting for a monitor lock. * A thread in the blocked state is waiting for a monitor lock * to enter a synchronized block/method or * reenter a synchronized block/method after calling * {@link Object#wait() Object.wait}. */ BLOCKED, // 阻塞 /** * Thread state for a waiting thread. * A thread is in the waiting state due to calling one of the * following methods: * <ul> * <li>{@link Object#wait() Object.wait} with no timeout</li> * <li>{@link #join() Thread.join} with no timeout</li> * <li>{@link LockSupport#park() LockSupport.park}</li> * </ul> * * <p>A thread in the waiting state is waiting for another thread to * perform a particular action. * * For example, a thread that has called <tt>Object.wait()</tt> * on an object is waiting for another thread to call * <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on * that object. A thread that has called <tt>Thread.join()</tt> * is waiting for a specified thread to terminate. */ WAITING, // 等待 /** * Thread state for a waiting thread with a specified waiting time. * A thread is in the timed waiting state due to calling one of * the following methods with a specified positive waiting time: * <ul> * <li>{@link #sleep Thread.sleep}</li> * <li>{@link Object#wait(long) Object.wait} with timeout</li> * <li>{@link #join(long) Thread.join} with timeout</li> * <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li> * <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li> * </ul> */ TIMED_WAITING, // 定時等待 /** * Thread state for a terminated thread. * The thread has completed execution. */ TERMINATED; // 結束狀態 }
綜上,其實線程的狀態有六種:
NEW 新建狀態,一般通過Thread thread = new Thread(runable);此時的線程屬于新建狀態。
RUNABLE 可運行狀態,當調用start時,線程進入RUNNABLE狀態,該狀態其實還包含兩個狀態,一種是被cpu選中正在運行中,另一種是未被cpu選中,處于就緒狀態。
BLOCKED 阻塞狀態, 一般可以通過調用sleep()方法來進入阻塞狀態,此時線程沒有釋放鎖資源,sleep到期時,繼續進入Runable狀態
WAITING 等待狀態, 一般可以通過調用wait()方法來進入等待狀態,此時釋放cpu,cpu去干其他事情,需要調用noitfy方法喚醒,喚醒后的線程為RUNABLE狀態。
TIMED_WAIRING 定時等待, 一般可以通過wait(long)方法進入定時等待。基本上同WAITING.
TERMINATED 結束狀態,RUNCABLE運行正常結束的線程的狀態就是TERMINATED
可以看出八股文不能亂背,之前傻呵呵背的八股文很有可能是錯誤的,比如線程的運行中狀態(RUNNING),其實這個狀態根本不存在,RUNABLE狀態就已經包含了RUNNNING狀態了。
再回到標題的問題,為什么不能多次調用start方法,原因其實源碼的注釋上已經說明了,
/** * This method is not invoked for the main method thread or "system" * group threads created/set up by the VM. Any new functionality added * to this method in the future may have to also be added to the VM. * * A zero status value corresponds to state "NEW". */
0狀態對應的是NEW,也就是說只有新建狀態的線程才能調用start方法,其他狀態的線程調用就會拋出異常,而一般第二次調用時,線程狀態肯定不是new狀態了。因此不可以多次調用。
經過多次的反復調試,原理其實很簡單,比如以下代碼:
public void testThreadPool() { ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 3, 10L, TimeUnit.SECONDS, new ArrayBlockingQueue(3)); threadPoolExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); for (int i=0; i<5; i++) { threadPoolExecutor.submit(new Runnable() { @Override public void run() { ThreadUtils.doSleep(10000L); System.out.println(Thread.currentThread().getName() + "--運行"); } }); } threadPoolExecutor.shutdown(); }
其中循環往threadPoolExecutor中添加的是自定義的業務任務。而真正去運行任務的是線程池中新建的一個線程。因此這里的復用指的是線程池創建出來得這個線程,這個線程并不會銷毀,而是循環去隊列中獲取任務。千萬不可理解為線程池復用的線程是使用者自定義的那個業務任務。具體的復用最核心的代碼就是下面這段:
while (task != null || (task = getTask()) != null) { w.lock(); // If pool is stopping, ensure thread is interrupted; // if not, ensure thread is not interrupted. This // requires a recheck in second case to deal with // shutdownNow race while clearing interrupt if ((runStateAtLeast(ctl.get(), STOP) || (Thread.interrupted() && runStateAtLeast(ctl.get(), STOP))) && !wt.isInterrupted()) wt.interrupt(); try { beforeExecute(wt, task); Throwable thrown = null; try { task.run(); } catch (RuntimeException x) { thrown = x; throw x; } catch (Error x) { thrown = x; throw x; } catch (Throwable x) { thrown = x; throw new Error(x); } finally { afterExecute(task, thrown); } } finally { task = null; w.completedTasks++; w.unlock(); } }
這段代碼是runworker中的一段代碼,線程就是通過循環去獲取隊列中的任務來達到線程復用的,前臺創建多個runable對象,將任務放到runable中,然后將runable放到隊列中,線程池創建線程,線程持續循環獲取隊列中的任務。這就是線程池的實現邏輯。
下面嘗試自己去實現一個線程池:該線程只是為了模擬線程池的運行,并未做線程安全的考慮,也未做非核心線程超時回收等功能。
package com.cz.lock.distributed.impl.redis; import java.util.List; import java.util.concurrent.*; /** * @program: Reids * @description: 自定義線程池 * @author: Cheng Zhi * @create: 2023-02-28 09:28 **/ public class JefThreadPoolExecutor extends AbstractExecutorService { /** * 使用隊列來保存現有的worker */ private final BlockingQueue<Worker> workers = new LinkedBlockingQueue<Worker>(); private static int coreThreadCount = 5; private static int maxThreadCount = 10; private static int defaultQueueSize = maxThreadCount * 5; private static BlockingQueue<Runnable> blockingQueue = new ArrayBlockingQueue<Runnable>(defaultQueueSize); /** * 默認線程池 */ JefThreadPoolExecutor() { this(coreThreadCount, maxThreadCount, blockingQueue); } /** * 可以自定義的線程池 * @param coreThreadCount * @param maxThreadCount * @param blockingQueue */ JefThreadPoolExecutor(int coreThreadCount, int maxThreadCount, BlockingQueue blockingQueue) { this.blockingQueue = blockingQueue; this.coreThreadCount = coreThreadCount; this.maxThreadCount = maxThreadCount; } @Override public void shutdown() { } @Override public List<Runnable> shutdownNow() { return null; } @Override public boolean isShutdown() { return false; } @Override public boolean isTerminated() { return false; } @Override public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException { return false; } @Override public void execute(Runnable command) { int currentWorkCount = workers.size(); // 當前創建的線程總數 if (currentWorkCount < coreThreadCount) { // 如果當前線程總數小于核心線程數,則新建線程 Worker worker = new Worker(command); final Thread thread = worker.thread; thread.start(); addWorker(worker); return; } if (!blockingQueue.offer(command) && currentWorkCount <= maxThreadCount) { // 隊列可以正常放入則返回true,如果滿了返回false // 隊列如果滿了,需要創建新的線程 Worker worker = new Worker(command); final Thread thread = worker.thread; thread.start(); addWorker(worker); return; } else if (currentWorkCount > maxThreadCount){ System.out.println("線程池滿了....沒有多余的線程了"); } } public void addWorker(Worker worker) { workers.add(worker); } public Runnable getTask() { Runnable poll = blockingQueue.poll(); return poll; } public void runWorker(Worker worker) { Runnable task = worker.firstTask; // 獲取到new Worker時傳入的那個任務,并在下面運行 if (task != null) { task.run(); } worker.firstTask = null; // 循環從隊列中獲取任務處理 while((task = getTask()) != null) { task.run(); } } /** * 匿名內部類 */ private class Worker implements Runnable{ volatile int state = 0; public Runnable firstTask; final Thread thread; public Worker(Runnable firstTask) { this.firstTask = firstTask; thread = new Thread(this); } @Override public void run() { runWorker(this); } } }
使用方式:
/** * 使用默認配置 */ public static void singleThreadPoolExecutor() { JefThreadPoolExecutor jefThreadPoolExecutor = new JefThreadPoolExecutor(); for (int i=0; i<10; i++) { jefThreadPoolExecutor.execute(new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getName() + "--運行"); } }); } } /** * 自定義配置 */ public static void diyThreadPoolExecutor() { JefThreadPoolExecutor jefThreadPoolExecutor = new JefThreadPoolExecutor(2, 10, new ArrayBlockingQueue(50)); for (int i=0; i<500; i++) { jefThreadPoolExecutor.execute(new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getName() + "--運行"); } }); } }
以上就是關于“Java中線程池自定義如何實現”這篇文章的內容,相信大家都有了一定的了解,希望小編分享的內容對大家有幫助,若想了解更多相關的知識內容,請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。