解决ScheduledThreadPoolExecutor关闭连接池方法

ScheduledThreadPoolExecutor exec = new ScheduledThreadPoolExecutor(1);
Map<ScheduledFuture, ThreadAlarm> maps = new HashMap<ScheduledFuture, ThreadAlarm>();

for (int i = 0; i < 5; i++) {

ThreadAlarm t = new ThreadAlarm();
// ThreadEx t = new ThreadEx();
t.setAlarm(new Alarm(String.valueOf(i),"张三"+String.valueOf(i),String.valueOf((i+1)*1000)));
ScheduledFuture sf = (ScheduledFuture<ThreadAlarm>) exec.scheduleAtFixedRate(t, 0, Long.parseLong(t.getAlarm().getTime()), TimeUnit.MILLISECONDS);
maps.put(sf, t);
}
BlockingQueue<Runnable> bq = exec.getQueue();
System.out.println("************************************");
for (Iterator iterator = bq.iterator(); iterator.hasNext();) {
Runnable runnable = (Runnable) iterator.next();
ScheduledFuture ft = (ScheduledFuture) runnable;
//System.out.println(ft);
ThreadAlarm ta = maps.get(ft);
System.out.println(ta.getAlarm().getId()+","+ta.getAlarm().getName());
if("1".equals(ta.getAlarm().getId())){
ft.cancel(false);
}
}
System.out.println("************************************");
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页