先看下下面这个用例
public class ExecutorsTest {
public static void main(String[] args) {
ThreadPoolTaskExecutor executorService = buildThreadPoolTaskExecutor();
executorService.execute(() -> test("execute"));
executorService.submit(() -> test("submit"));
}
private static void test(String name) {
String printStr = "【thread-name:" + Thread.currentThread().getName() + ",执行方式:" + name + "】";
System.out.println(printStr);
throw new RuntimeException(printStr + "异常");
}
private static ThreadPoolTaskExecutor buildThreadPoolTaskExecutor() {
ThreadPoolTaskExecutor executorService = new ThreadPoolTaskExecutor();
executorService.setThreadNamePrefix("test");
executorService.setCorePoolSize(5);
executorService.setMaxPoolSize(10);
executorService.setQueueCapacity(1000);
executorService.setKeepAliveSeconds(30);
executorService.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
executorService.initialize();
return executorService;
}
}
看下执行结果
【thread-name:test1,执行方式:execute】
【thread-name:test3,执行方式:submit】
Exception in thread "test1" java.lang.RuntimeException: 【thread-name:test1,执行方式:execute】异常
at com.test.thread.ExecutorsTest.test(ExecutorsTest.java:22)
at com.test.thread.ExecutorsTest.lambda$main$0(ExecutorsTest.java:15)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
当执行方式是execute时,可以看到堆栈异常的输出。
当执行方式是submit时,堆栈异常没有输出。
那么我们怎么拿到submit执行方式的堆栈异常呢?
public static void main(String[] args) {
ThreadPoolTaskExecutor executorService = buildThreadPoolTaskExecutor();
executorService.execute(() -> test("execute"));
//拿到sumit的返回值Future对象
Future<?> submit = executorService.submit(() -> test("submit"));
try {
//通过Future.get()方法拿到任务的返回值
submit.get();
} catch (Exception e) {
e.printStackTrace();
}
}
看下执行结果
Exception in thread "test1" java.lang.RuntimeException: 【thread-name:test1,执行方式:execute】异常
at com.test.thread.ExecutorsTest.test(ExecutorsTest.java:29)
at com.test.thread.ExecutorsTest.lambda$main$0(ExecutorsTest.java:17)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
java.util.concurrent.ExecutionException: java.lang.RuntimeException: 【thread-name:test3,执行方式:submit】异常
at java.util.concurrent.FutureTask.report(FutureTask.java:122)
at java.util.concurrent.FutureTask.get(FutureTask.java:192)
at com.test.thread.ExecutorsTest.main(ExecutorsTest.java:20)
Caused by: java.lang.RuntimeException: 【thread-name:test3,执行方式:submit】异常
at com.test.thread.ExecutorsTest.test(ExecutorsTest.java:29)
at com.test.thread.ExecutorsTest.lambda$main$1(ExecutorsTest.java:18)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
execute方法执行时,会抛出(打印)堆栈异常。
submit方法执行时,返回结果封装在future中,如果调用Future.get()方法则必须进行异常捕获,从而可以抛出(打印)堆栈异常。
为啥execute直接抛出异常,submit没有直接抛出异常呢?
execute方法执行
最终都是在java.util.concurrent.ThreadPoolExecutor#runWorker
方法中执行
final void runWorker(Worker w) {
Thread wt = Thread.currentThread();
Runnable task = w.firstTask;
w.firstTask = null;
w.unlock(); // allow interrupts
boolean completedAbruptly = true;
try {
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 {
//最终执行的是runnable.run()方法
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();
}
}
completedAbruptly = false;
} finally {
processWorkerExit(w, completedAbruptly);
}
}
submit方法执行
org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor#submit(java.lang.Runnable)
public Future<?> submit(Runnable task) {
ExecutorService executor = getThreadPoolExecutor();
try {
return executor.submit(task);
}
catch (RejectedExecutionException ex) {
throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex);
}
}
java.util.concurrent.AbstractExecutorService#submit(java.lang.Runnable)
public Future<?> submit(Runnable task) {
if (task == null) throw new NullPointerException();
RunnableFuture<Void> ftask = newTaskFor(task, null);
execute(ftask);
return ftask;
}
可以看到这里执行的task的类型是FutureTask
最终在java.util.concurrent.ThreadPoolExecutor#runWorker
方法中执行task.run()
执行的是FutureTask.run()方法
java.util.concurrent.FutureTask#run
public void run() {
if (state != NEW ||
!UNSAFE.compareAndSwapObject(this, runnerOffset,
null, Thread.currentThread()))
return;
try {
Callable<V> c = callable;
if (c != null && state == NEW) {
V result;
boolean ran;
try {
//执行调用逻辑
result = c.call();
ran = true;
} catch (Throwable ex) {
//捕获异常,没有抛出
result = null;
ran = false;
//保存异常
setException(ex);
}
if (ran)
set(result);
}
} finally {
// runner must be non-null until state is settled to
// prevent concurrent calls to run()
runner = null;
// state must be re-read after nulling runner to prevent
// leaked interrupts
int s = state;
if (s >= INTERRUPTING)
handlePossibleCancellationInterrupt(s);
}
}
java.util.concurrent.FutureTask#setException
protected void setException(Throwable t) {
//通过CAS将状态stateOffset从NEW变为COMPLETING
if (UNSAFE.compareAndSwapInt(this, stateOffset, NEW, COMPLETING)) {
//将异常赋值给outcome
outcome = t;
//将stateOffset改为EXCEPTIONAL
UNSAFE.putOrderedInt(this, stateOffset, EXCEPTIONAL); // final state
finishCompletion();
}
}
private static final int NEW = 0;
private static final int COMPLETING = 1;
private static final int NORMAL = 2;
private static final int EXCEPTIONAL = 3;
private static final int CANCELLED = 4;
private static final int INTERRUPTING = 5;
private static final int INTERRUPTED = 6;
通过Future.get()
获取调用结果的时候
java.util.concurrent.FutureTask#get()
public V get() throws InterruptedException, ExecutionException {
//此时state = 3
int s = state;
if (s <= COMPLETING)
s = awaitDone(false, 0L);
//调用report方法
return report(s);
}
java.util.concurrent.FutureTask#report
private V report(int s) throws ExecutionException {
//outcome就是之前赋值的异常
Object x = outcome;
//s就是state=3 都不满足条件
if (s == NORMAL)
return (V)x;
if (s >= CANCELLED)
throw new CancellationException();
//抛出异常
throw new ExecutionException((Throwable)x);
}
总结
- 当执行方式是execute时,可以看到堆栈异常的输出。
- 当执行方式是submit时,堆栈异常没有输出。但是调用
Future.get()
方法时,可以捕获到异常。 - 不会影响线程池里面其他线程的正常执行。
- 线程池会把这个线程移除掉,并创建一个新的线程放到线程池中。