新线程内没有任何资源,需要的dao、service都需要从主线程传进去;
开启线程后,主线程并没有结束,会一直等待,但是junit单元测试不同,一旦开启新线程,不管新线程是否执行结束,主线程执行完就会结束。
线程方法:
public class AutoCreateAtx implements Runnable {
private ServerCall sc;
private OpenAccountTx tx;
public AutoCreateAtx(ServerCall sc, OpenAccountTx tx) {
this.sc = sc;
this.tx = tx;
}
/**
* @Description:
* @author wqx
* @since 2016-7-20 上午05:24:47
*/
public void run() {
try {
((RemoteCallGatewayImpl) AppContextUtils.getApplicationContext().getBean("cpm.CPMServerGateway")).doCommand(sc);
} catch (Exception t) {
tx.setErrorText(t.getMessage());
t.printStackTrace();
CPM.getHBSession().save(tx);
PersistTools.flushSession();
}
}
}
调用逻辑:
ExecutorService executorService = Executors.newFixedThreadPool(5);
executorService.execute(new AutoCreateAtx(atxCall, tx));
原文:http://www.cnblogs.com/wqingxia/p/5728326.html