/*** 过期数据清理守护线程*/private ScheduledThreadPoolExecutor scheduler = new ScheduledThreadPoolExecutor(1, r -> {Thread thread = new Thread(r, "Thread-Idempotency-deamon");thread.setDaemon(true);return thread;});
使用
private void initDeamon(){scheduler.scheduleWithFixedDelay(()->{for (Entry<String, Long> entry : dataMap.entrySet()) {if(System.currentTimeMillis() > entry.getValue()){dataMap.remove(entry.getKey());}}// 处理第三方清理逻辑deleteOther();}, 0, 5, TimeUnit.SECONDS);}
private final ScheduledExecutorService executor;...//create after the call above where MalformedURLException can be raised//avoids having to call shutdown in case the exception is raisedexecutor = Executors.newSingleThreadScheduledExecutor(r -> {Thread ret = new Thread(r, "Executor for Watch " + System.identityHashCode(WatchConnectionManager.this));ret.setDaemon(true);return ret;});
