国产毛多水多高潮高清,久热这里只有精品视频6,国内精品久久久久久久久电影网,国产男同志CHINA69,精品999日本久久久影院,人人妻人人澡人人爽人人精品,亚洲中文无码永久免

如何优雅的关闭 Java线程池-AG亚游国际平台APP

如何优雅的关闭 Java线程池

2026-01-18 02:59:12投稿人:火狐體育平臺登錄(黃石)有限公司圍觀7399163 評論

如何優(yōu)雅的關閉 Java線程池

簡介 在開發(fā)中使用線程池去執(zhí)行異步任務是比較普遍的操作,然而雖然有些異步操作我們并不十分要求可靠性和實時性 ,但總歸業(yè)務還是需要的 。如果在每次的服務發(fā)版過程中,我們不去介入線程池的停機邏輯,那么很有可能就會造成線程池中隊列的任務還未執(zhí)行完成,自然就會造成數據的丟失 。

探究

注意,本文所有前提是對進程進行下線時使用的是kill -15

我們知道Spring已經實現(xiàn)了自己的優(yōu)雅停機方案,詳細請參考org.springframework.context.support.AbstractApplicationContext#registerShutdownHook,然后主要看調用的org.springframework.context.support.AbstractApplicationContext#doClose , 在這個方法里定義了容器銷毀的執(zhí)行順序

protected void doClose() {   // Check whether an actual close attempt is necessary...  if (this.active.get() && this.closed.compareAndSet(false, true)) {    if (logger.isDebugEnabled()) {     logger.debug("Closing " + this);   }   LiveBeansView.unregisterApplicationContext(this);   try {     // Publish shutdown event.    publishEvent(new ContextClosedEvent(this));   }   catch (Throwable ex) {     logger.warn("Exception thrown from ApplicationListener handling ContextClosedEvent", ex);   }   // Stop all Lifecycle beans, to avoid delays during inpidual destruction.   if (this.lifecycleProcessor != null) {     try {      this.lifecycleProcessor.onClose();    }    catch (Throwable ex) {      logger.warn("Exception thrown from LifecycleProcessor on context close", ex);    }   }   // Destroy all cached singletons in the context's BeanFactory.   destroyBeans();   // Close the state of this context itself.   closeBeanFactory();   // Let subclasses do some final clean-up if they wish...   onClose();   // Reset local application listeners to pre-refresh state.   if (this.earlyApplicationListeners != null) {     this.applicationListeners.clear();    this.applicationListeners.addAll(this.earlyApplicationListeners);   }   // Switch to inactive.   this.active.set(false);  } }

我們先主要關注下destroyBeans這個方法