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; } } }