我测试验证了下,主线程会等待,因为ScheduledExecutorService没有被shutdown,你的需求是什么?如果你希望执行结束后结束进行,那么shutdown即可。 你参考下以下例子: public static void main(String[] args) throws IOException { Object agentInfo = null; Integer samplingInterval = 30; ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(10); executorService.schedule(new WatchAgent(agentInfo), 1, TimeUnit.SECONDS); // executorService.scheduleAtFixedRate(new WatchAgent(agentInfo), 0, samplingInterval, TimeUnit.SECONDS); System.err.println("FINISH"); } static class WatchAgent implements Runnable { public WatchAgent(Object info){ } public void run(){ try{ System.out.println("Running " + this.hashCode() + " - started on/at " + (new Date())); Thread.sleep(6000); System.out.println("Running " + this.hashCode() + " - finished on/at " + (new Date())); }catch(Exception ex){ ex.printStackTrace(); } } }
我测试验证了下,主线程会等待,因为ScheduledExecutorService没有被shutdown,你的需求是什么?如果你希望执行结束后结束进行,那么shutdown即可。
你参考下以下例子: