seo网站推广软件 快排,手机网页小游戏,做响应式的网站,长沙做网站哪家好JAVA线程执行中断方式
Java中只能通过协作的方式取消 第一种是通过标志位实现#xff0c;假设有个计算所有素数的任务#xff0c;每次计算前检查下是否取消的标志位#xff0c;如果为true则退出计算。调用方想要取消任务的话#xff0c;则将标志位设为true。但这种方法无法…JAVA线程执行中断方式
Java中只能通过协作的方式取消 第一种是通过标志位实现假设有个计算所有素数的任务每次计算前检查下是否取消的标志位如果为true则退出计算。调用方想要取消任务的话则将标志位设为true。但这种方法无法再计算的过程中取消任务像是一些阻塞调用无法被取消 第二种是中断用于通过协作机制停止线程继续执行任务原理是向进程发送中断请求将标记线程为Interrupted线程会在下一个合适的时刻停止运行阻塞的库方法例如Thread.sleep Object.wait 都会响应中断抛出InterruptedException意味着阻塞操作因为中断结束但不能保证响应速度。 通常任务不会在自己拥有的线程执行而是在某个服务的线程池中执行因此任务应该保存阻塞状态使得线程能处理中断。这就是库函数抛出InterruptedException的原因。同时任务也不能对线程处理中断的策略做任何假设。 有两种处理中断的方法千万不要catch掉异常什么也不做除非你拥有该线程 向外抛出InterruptedException调用 Thread.currentThread().interrupt();恢复中断状态 例如在向线程池提交runnable执行时runnable调用了阻塞库函数则需要写处理中断的逻辑逻辑中要使用上面处理中断的两种方法之一因为线程池会处理线程遇到阻塞将线程从可用线程列表删除统计审计信息等操作 final void runWorker(Worker w) {Thread wt Thread.currentThread();Runnable task w.firstTask;w.firstTask null;w.unlock(); // allow interruptsboolean completedAbruptly true;try {while (task ! null || (task getTask()) ! null) {w.lock();try {beforeExecute(wt, task);Throwable thrown null;try {task.run(); // 执行runnable} 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); // 处理InteruptException}}
private void processWorkerExit(Worker w, boolean completedAbruptly) {if (completedAbruptly) // If abrupt, then workerCount wasnt adjusteddecrementWorkerCount();final ReentrantLock mainLock this.mainLock;mainLock.lock();try {completedTaskCount w.completedTasks;workers.remove(w);} finally {mainLock.unlock();}tryTerminate();int c ctl.get();if (runStateLessThan(c, STOP)) {if (!completedAbruptly) {int min allowCoreThreadTimeOut ? 0 : corePoolSize;if (min 0 ! workQueue.isEmpty())min 1;if (workerCountOf(c) min)return; // replacement not needed}addWorker(null, false);}}还有一种方法是使用future来中断任务执行使用submit.cancel(true)内部也是利用了Thread.interrupt
ExecutorService executor Executors.newFixedThreadPool(1);final Future? submit executor.submit(() - {});try {final Object o submit.get(10, TimeUnit.MILLISECONDS);} catch (TimeoutException e) {}finally {submit.cancel(true); // 正常得到结果后再cancel也无影响}未捕获异常的处理方式
线程因为异常或者error退出后会向System.error输出堆栈但线上程序一般通过日志来排查问题而不是控制台输出。Java支持配置Thread.UncaughtExceptionHandler处理这种未被捕获的Throwable程序最好自己实现该接口至少将异常堆栈输出到日志中下面是ES实现的Thread.UncaughtExceptionHandler(去掉了一些细节)ES将异常信息输出到了日志文件。
class ElasticsearchUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {private static final Logger logger LogManager.getLogger(ElasticsearchUncaughtExceptionHandler.class);Overridepublic void uncaughtException(Thread thread, Throwable t) {if (isFatalUncaught(t)) {onFatalUncaught(thread.getName(), t);} else {onNonFatalUncaught(thread.getName(), t);}}static boolean isFatalUncaught(Throwable e) {return e instanceof Error;}void onFatalUncaught(final String threadName, final Throwable t) {final String message fatal error in thread [ threadName ], exiting;logger.error(message, t);Terminal.DEFAULT.errorPrintln(message);t.printStackTrace(Terminal.DEFAULT.getErrorWriter());// Without a final flush, the stacktrace may not be shown before ES exitsTerminal.DEFAULT.flush();}void onNonFatalUncaught(final String threadName, final Throwable t) {final String message uncaught exception in thread [ threadName ];logger.error(message, t);Terminal.DEFAULT.errorPrintln(message);t.printStackTrace(Terminal.DEFAULT.getErrorWriter());// Without a final flush, the stacktrace may not be shown if ES goes on to exitTerminal.DEFAULT.flush();}}