package com.umeox.babywei.k3.service;import java.util.concurrent.Callable;import java.util.concurrent.Future;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.DisposableBean;import org.springframework.beans.factory.InitializingBean;import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;import org.springframework.stereotype.Component;@Componentpublic class JdbcExecutor implements InitializingBean, DisposableBean { protected Logger logger = LoggerFactory.getLogger(this.getClass()); public static JdbcExecutor instance; private ThreadPoolTaskExecutor jdbcExecutor = null; private volatile boolean stopping = false; @Override public void afterPropertiesSet() throws Exception { instance = this; int limit = 100; // 實(shí)際掃描線程池 jdbcExecutor = new ThreadPoolTaskExecutor(); jdbcExecutor.setCorePoolSize(limit / 5); jdbcExecutor.setMaxPoolSize(limit); jdbcExecutor.setWaitForTasksToCompleteOnShutdown(true); jdbcExecutor.afterPropertiesSet(); // Thread thread = new Thread(new Runnable() { // @Override // public void run() { // while (!stopping) { // // logger.info("JdbcExecutor Status\n. {}", // jdbcExecutor.getThreadPoolExecutor()); // // try { // Thread.sleep(60 * 1000); // } catch (InterruptedException e) { // e.printStackTrace(); // } // } // } // }); // // thread.start(); } public void submit(Runnable runnable) { this.jdbcExecutor.submit(runnable); } @Override public void destroy() throws Exception { stopping = true; } public Future<?> submit(Callable<?> callable) { return this.jdbcExecutor.submit(callable); }}
3 回答

冉冉說
TA貢獻(xiàn)1877條經(jīng)驗(yàn) 獲得超1個(gè)贊
猜測(cè)這個(gè)作者的意圖是希望在當(dāng)前類的一個(gè)實(shí)例屬性被設(shè)置后
(afterPropertiesSet
)激活一個(gè)當(dāng)前類的靜態(tài)實(shí)例指向該實(shí)例。
他允許創(chuàng)建多個(gè)實(shí)例,但靜態(tài)的instance永遠(yuǎn)指向最后一個(gè)調(diào)用afterPropertiesSet
的那一個(gè)。
不知道他為什么這么寫,也許是業(yè)務(wù)需要,但可能單例模式更好一點(diǎn)。

慕妹3146593
TA貢獻(xiàn)1820條經(jīng)驗(yàn) 獲得超9個(gè)贊
添加回答
舉報(bào)
0/150
提交
取消