我在关闭 ExecutorService 已终止的应用程序时遇到问题......有什么好的方法来处理这个问题?
public class TradingLock {
private ExecutorService executorService;
private List<TradingHaltedListener> listeners=new ArrayList<>();
public TradingLock(ExecutorService executorService) {
super();
this.executorService = executorService;
}
public void haltTrading(){
for (final TradingHaltedListener listener: listeners){
executorService.execute(new Runnable() {
@Override
public void run() {
listener.onTradingHalted();
}
});
}
}
public synchronized void addTradingHaltedListener(TradingHaltedListener listener){
this.listeners.add(listener);
}
}
主类的关闭钩子:
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
tradingLock.haltTrading();
}
});