ExecutorService handlePool = Executors.newFixedThreadPool(sinkThreadNum); for (int i = 0; i < sinkThreadNum; ++i) { Thread thread = new SinkTask(i); thread.setDaemon(true); handlePool.submit(thread); } latch.await(); Thread.sleep(1000*60); handlePool.shutdown(); //进程不会退出 //handlePool.shutdownNow(); // 强制关闭资源,进程退出 public void run() { MetricValue metricValue = null; BlockingQueue<MetricValue> queue = queues.getQueue(index); while (true) { try { metricValue = queue.poll(); if (metricValue == null) { System.out.println("queue 已经空了, index=" + this.index); metricValue = queue.take(); // 线程被阻塞在这里,使进程不会退出。 handlePool.shutdown()并不能清理资源。 // shutdownNow()会给线程发中断信号 } doWork(metricValue); } catch (Exception e) { return; } } }