ThreadPoolExecutor应用
任务类型
1、java.lang.Runnable
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | @FunctionalInterface public interface Runnable { /** * When an object implementing interface <code>Runnable</code> is used * to create a thread, starting the thread causes the object's * <code>run</code> method to be called in that separately executing * thread. * <p> * The general contract of the method <code>run</code> is that it may * take any action whatsoever. * * @see java.lang.Thread#run() */ public abstract void run(); } |
2、java.util.concurrent.Callable
1 2 3 4 5 6 7 8 9 10 | @FunctionalInterface public interface Callable<V> { /** * Computes a result, or throws an exception if unable to do so. * * @return computed result * @throws Exception if unable to compute a result */ V call() throws Exception; } |
任务提交
方式
1、java.util.concurrent.ThreadPoolExecutor#execute
public void execute(Runnable command) {}
2、java.util.concurrent.AbstractExecutorService#submit(java.lang.Runnable)
1 2 3 4 5 6 | public Future<?> submit(Runnable task) { if (task == null ) throw new NullPointerException(); RunnableFuture<Void> ftask = newTaskFor(task, null ); execute(ftask); return ftask; } |
3、java.util.concurrent.AbstractExecutorService#submit(java.lang.Runnable, T)
1 2 3 4 5 6 | public <T> Future<T> submit(Runnable task, T result) { if (task == null ) throw new NullPointerException(); RunnableFuture<T> ftask = newTaskFor(task, result); execute(ftask); return ftask; } |
4、java.util.concurrent.AbstractExecutorService#submit(java.util.concurrent.Callable<T>)
1 2 3 4 5 6 | public <T> Future<T> submit(Callable<T> task) { if (task == null ) throw new NullPointerException(); RunnableFuture<T> ftask = newTaskFor(task); execute(ftask); return ftask; } |
任务提交拒绝策略
1、java.util.concurrent.ThreadPoolExecutor#execute
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | // command the task to execute // @throws RejectedExecutionException at discretion of {@code // RejectedExecutionHandler}, if the task cannot be accepted for execution public void execute(Runnable command) { if (command == null ) throw new NullPointerException(); int c = ctl.get(); if (workerCountOf(c) < corePoolSize) { if (addWorker(command, true )) return ; c = ctl.get(); } if (isRunning(c) && workQueue.offer(command)) { int recheck = ctl.get(); if (! isRunning(recheck) && remove(command)) reject(command); -- 拒绝 else if (workerCountOf(recheck) == 0 ) addWorker( null , false ); } else if (!addWorker(command, false )) reject(command); -- 拒绝 } |
2、java.util.concurrent.AbstractExecutorService#submit(java.lang.Runnable)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | /** * @throws RejectedExecutionException {@inheritDoc} * @throws NullPointerException {@inheritDoc} */ public Future<?> submit(Runnable task) { if (task == null ) throw new NullPointerException(); RunnableFuture<Void> ftask = newTaskFor(task, null ); -- 将Runnable包装 execute(ftask); -- java.util.concurrent.ThreadPoolExecutor#execute return ftask; } protected <T> RunnableFuture<T> newTaskFor(Runnable runnable, T value) { return new FutureTask<T>(runnable, value); } |
3、java.util.concurrent.AbstractExecutorService#submit(java.lang.Runnable, T)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | /** * @throws RejectedExecutionException {@inheritDoc} * @throws NullPointerException {@inheritDoc} */ public <T> Future<T> submit(Runnable task, T result) { if (task == null ) throw new NullPointerException(); RunnableFuture<T> ftask = newTaskFor(task, result); // 将Runnable包装 execute(ftask); // java.util.concurrent.ThreadPoolExecutor#execute return ftask; } protected <T> RunnableFuture<T> newTaskFor(Runnable runnable, T value) { return new FutureTask<T>(runnable, value); } |
4、java.util.concurrent.AbstractExecutorService#submit(java.util.concurrent.Callable<T>)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | /** * @throws RejectedExecutionException {@inheritDoc} * @throws NullPointerException {@inheritDoc} */ public <T> Future<T> submit(Callable<T> task) { if (task == null ) throw new NullPointerException(); RunnableFuture<T> ftask = newTaskFor(task); // 将Callable包装 execute(ftask); //java.util.concurrent.ThreadPoolExecutor#execute return ftask; } protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) { return new FutureTask<T>(callable); } |
【execute、submit时,可能会有异常,需要手动try...catch...】
任务线程异常
java.util.concurrent.ThreadPoolExecutor#execute
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | public class ThreadPoolExecutor extends AbstractExecutorService { private final class Worker extends AbstractQueuedSynchronizer implements Runnable{ public void run() { runWorker( this ); } } final void runWorker(Worker w) { ... beforeExecute(wt, task); run... throw ... ... afterExecute(task, thrown); ... } } |
任务线程执行异常,将由 java.lang.Thread.UncaughtExceptionHandler 进行处理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | // When a thread is about to terminate due to an uncaught exception // the Java Virtual Machine will query the thread for its // UncaughtExceptionHandler using getUncaughtExceptionHandler // and will invoke the handler's uncaughtException method, // passing the thread and the exception as arguments. @FunctionalInterface public interface UncaughtExceptionHandler { /** * Method invoked when the given thread terminates due to the * given uncaught exception. * <p>Any exception thrown by this method will be ignored by the * Java Virtual Machine. * @param t the thread * @param e the exception */ void uncaughtException(Thread t, Throwable e); } |
1 2 3 4 5 6 7 | /** * Dispatch an uncaught exception to the handler. This method is * intended to be called only by the JVM. */ private void dispatchUncaughtException(Throwable e) { getUncaughtExceptionHandler().uncaughtException( this , e); } |
当任务线程异常,JVM会调用dispatchUncaughtException处理异常
用户可以自定义UncaughtExceptionHandler,实现UncaughtExceptionHandler,通过java.lang.Thread#setUncaughtExceptionHandler自定义线程异常处理器
1 2 3 4 | public void setUncaughtExceptionHandler(UncaughtExceptionHandler eh) { checkAccess(); uncaughtExceptionHandler = eh; } |
java.util.concurrent.AbstractExecutorService#submit(java.lang.Runnable)/submit(java.lang.Runnable, T)/submit(java.util.concurrent.Callable<T>)
submit方式,将Runnable、Callble包装成java.util.concurrent.FutureTask,任务执行体就是FutureTask的run方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | public interface RunnableFuture<V> extends Runnable, Future<V> { void run(); } public class FutureTask<V> implements RunnableFuture<V> { // The result to return or exception to throw from get() private Object outcome; public void run() { ... try { result = c.call(); // 任务执行 ... } catch (Throwable ex) { result = null ; ran = false ; setException(ex); // 任务异常处理 } .... } protected void setException(Throwable t) { if (UNSAFE.compareAndSwapInt( this , stateOffset, NEW, COMPLETING)) { outcome = t; // 将异常信息存储到outcome UNSAFE.putOrderedInt( this , stateOffset, EXCEPTIONAL); // final state finishCompletion(); } } } |
当调用java.util.concurrent.FutureTask#get(),任务线程的异常就会获取到
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | public V get() throws InterruptedException, ExecutionException { int s = state; if (s <= COMPLETING) s = awaitDone( false , 0L); return report(s); // 获取异常信息 } /** * Returns result or throws exception for completed task. * * @param s completed state value */ @SuppressWarnings ( "unchecked" ) private V report( int s) throws ExecutionException { Object x = outcome; // 从outcome获取异常信息 if (s == NORMAL) return (V)x; if (s >= CANCELLED) throw new CancellationException(); throw new ExecutionException((Throwable)x); } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)