ThreadPoolTaskScheduler 簡單的記錄

initializeBean方法:

protected Object initializeBean(final String beanName, final Object bean, @Nullable RootBeanDefinition mbd) {
		// 省略。。。。
        // 對於實現BeanNameAware接口的類,將執行setBeanName方法,
        // 我覺的setBeanName的作用更傾向於日誌方面的記錄,比如Spring async線程池 
        // taskScheduler名稱
        // 的日誌打印記錄,就是用了這個方法,獲取name信息
	    invokeAwareMethods(beanName, bean);
		
		Object wrappedBean = bean;
		if (mbd == null || !mbd.isSynthetic()) {
           // @PostConstruct執行時機,如果想再項目啓動後執行某些操作,
            //使用 CommandLineRunner或ApplicationRunner
            // 這倆方法差不多,都是在refresh執行後執行callRunners進行調用的
 			wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
		}

		try {
            //調用初始化方法invokeInitMethods,在invokeInitMethods中調用實現了                    
            //InitializingBean的afterPropertiesSet方法。
			invokeInitMethods(beanName, wrappedBean, mbd);
		}
		catch (Throwable ex) {
			throw new BeanCreationException(
					(mbd != null ? mbd.getResourceDescription() : null),
					beanName, "Invocation of init method failed", ex);
		}
		if (mbd == null || !mbd.isSynthetic()) {
			wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
		}

		return wrappedBean;
	}
TaskExecutor:函數式接口實現了Executor
SchedulingTaskExecutor:接口默認default方法,執行短任務優先,目前未發現用處
CustomizableThreadCreator:負責線程創建,默認用戶線程daemon=false
ExecutorConfigurationSupport:線程池配置和生命週期管理

 

銷燬方法:

public void shutdown() {
		if (logger.isInfoEnabled()) {
			logger.info("Shutting down ExecutorService" + (this.beanName != null ? " '" + this.beanName + "'" : ""));
		}
		if (this.executor != null) {
			if (this.waitForTasksToCompleteOnShutdown) {
				this.executor.shutdown();
			}
			else {
				for (Runnable remainingTask : this.executor.shutdownNow()) {
					cancelRemainingTask(remainingTask);
				}
			}
			awaitTerminationIfNecessary(this.executor);
		}
	}
private void awaitTerminationIfNecessary(ExecutorService executor) {
		if (this.awaitTerminationSeconds > 0) {
			try {
				if (!executor.awaitTermination(this.awaitTerminationSeconds, TimeUnit.SECONDS)) {
					if (logger.isWarnEnabled()) {
						logger.warn("Timed out while waiting for executor" +
								(this.beanName != null ? " '" + this.beanName + "'" : "") + " to terminate");
					}
				}
			}
			catch (InterruptedException ex) {
				if (logger.isWarnEnabled()) {
					logger.warn("Interrupted while waiting for executor" +
							(this.beanName != null ? " '" + this.beanName + "'" : "") + " to terminate");
				}
				Thread.currentThread().interrupt();
			}
		}
	}

對於flume的線程生命週期停止的過程

public void stop() {
    LOGGER.info("Configuration provider stopping");

    executorService.shutdown();
    try {
      if (!executorService.awaitTermination(500, TimeUnit.MILLISECONDS)) {
        LOGGER.debug("File watcher has not terminated. Forcing shutdown of executor.");
        executorService.shutdownNow();
        while (!executorService.awaitTermination(500, TimeUnit.MILLISECONDS)) {
          LOGGER.debug("Waiting for file watcher to terminate");
        }
      }
    } catch (InterruptedException e) {
      LOGGER.debug("Interrupted while waiting for file watcher to terminate");
      Thread.currentThread().interrupt();
    }
    lifecycleState = LifecycleState.STOP;
    LOGGER.debug("Configuration provider stopped");
  }

與netty、flume相比,spring線程池的生命週期管理,覺得更加面向對象,裏邊的設計還是很漂亮的

參考:

關閉線程池 shutdown 和 shutdownNow 的區別

http://www.matools.com/api/java8

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章