ExecutorService.shutdown()应该是在线程执行完毕后,才会去关闭

ExecutorService.shutdown()应该是在线程执行完毕后,才会去关闭。 
但是我用了计数信号量Semaphore后,发现线程还没有跑完,他就执行了shutdown(). 


Java代码   收藏代码
  1. import java.util.concurrent.ExecutorService;  
  2. import java.util.concurrent.Executors;  
  3. import java.util.concurrent.Semaphore;  
  4.   
  5. public class SemaphoreTest extends Thread {  
  6.     Semaphore position;  
  7.     private int id;  
  8.   
  9.     public SemaphoreTest(int i, Semaphore s) {  
  10.         this.id = i;  
  11.         this.position = s;  
  12.     }  
  13.   
  14.     public void run() {  
  15.         try {  
  16.             if (position.availablePermits() > 0) {  
  17.                 System.out.println("顾客[" + id + "]进入厕所,有空位");  
  18.             } else {  
  19.                 System.out.println("顾客[" + id + "]进入厕所,没空位,排队");  
  20.             }  
  21.             position.acquire();  
  22.             System.out.println("【" + id + "】acquire坑位");  
  23.             Thread.sleep((int) (Math.random() * 1000));  
  24.             System.out.println("【" + id + "】完毕release");  
  25.             position.release();  
  26.         } catch (Exception e) {  
  27.             e.printStackTrace();  
  28.         }  
  29.     }  
  30.   
  31.     public static void main(String args[]) {  
  32.         ExecutorService pool = Executors.newCachedThreadPool();  
  33.         Semaphore position = new Semaphore(2); // 初始化两个空位  
  34.         for (int i = 0; i < 5; i++) {  
  35.             pool.submit(new SemaphoreTest(i, position));  
  36.         }  
  37.         System.out.println("开始释放线程池资源");  
  38.         pool.shutdown();  
  39.         System.out.println("完成释放线程池资源");  
  40.         position.acquireUninterruptibly(2);  
  41.         System.out.println("如厕完毕,清理厕所");  
  42.         position.release(2);  
  43.     }  
  44. }  


PS: 
1、代码中主线程并没有等待线程池执行完毕这一说,而是持续往下执行 
2、至于信号量,只会在几个子线程之间发挥作用 
3、主线程和线程池之间没有直接关系,线程池使用自己的线程。生命周期也相互独立。 
4、shutdown()可以理解为:主线程要求线程池关闭,但不会为此等待线程池执行完毕。 
5、实际发挥了等待作用的并不是线程池所提供的能力(当然线程池也确实提供了这类能力),而是:position.acquireUninterruptibly(2) 这句话。 
shutdown() 作为函数,当然是立即执行,也即是不再接受新任务了;但是它即不会强行终止正在执行的任务,也不会取消已经提交的任务。也就是说之前提交的5个任务,仍然会执行完毕,且跟主线程生命周期无关,也就是即便你直接在后面写上: if (1==1) return; 来立即结束主函数,你也会发现线程池的5个任务会顺利执行完毕。 
6、另一个长得很像的函数是: 
shutdownNow(),这个函数比shutdown()更狠,两点: 
1)、对于尚未执行的任务,全部取消掉; 
2)、对于正在执行的任务,发出interrupt()。 
不过程序因为在发生异常时没有正确释放信号量(应该放在finally块中释放),所以如果改为shutdownNow()会出问题:主线程死等。 


Java5 并发学习  
ExecutorService中submit和execute的区别  
Java代码   收藏代码
  1. public List<DrugDpInfoVO> selectRecipeSumVO(final AlertMessageKeyword query, PageBreaker page, final Integer engineRunFlag) {  
  2.     List<DrugDpInfoVO> list = optAlertmessageStatisticMapper.selectRecipeSumVO(this.getBaseDBName(), query, page, engineRunFlag);  
  3.   
  4.     if (null == list || list.isEmpty()) {  
  5.         log.warn("无匹配记录");  
  6.         return list;  
  7.     }  
  8.   
  9.     //ExecutorService EXECUTORSERVICE = Executors.newFixedThreadPool(50);  
  10.     List<Future<DrugDpInfoVO>> listFuture = new ArrayList<>();  
  11.   
  12.     try {  
  13.         for (final DrugDpInfoVO e : list) {  
  14.             Future<DrugDpInfoVO> future = EXECUTORSERVICE.submit(new Callable<DrugDpInfoVO>() {  
  15.   
  16.                 @Override  
  17.                 public DrugDpInfoVO call() throws Exception {  
  18.   
  19.                     DrugDpInfoVO drugDpInfoVO = optAlertmessageStatisticMapper.selectSingleRecipeSumVO(getBaseDBName(), query, e, engineRunFlag);  
  20.                     drugDpInfoVO.setAmount(e.getAmount());  
  21.                     return drugDpInfoVO;  
  22.   
  23.                 }  
  24.             });  
  25.   
  26.             listFuture.add(future);  
  27.         }  
  28.   
  29.         list.clear();  
  30.   
  31.         for (Future<DrugDpInfoVO> future : listFuture) {  
  32.             list.add(future.get());  
  33.         }  
  34.   
  35.     } catch (Exception e) {  
  36.         log.error(e.getMessage(), e);  
  37.     }  
  38.   
  39.     return list;  
  40. }  




猜你喜欢

转载自blog.csdn.net/weikeke410/article/details/51496676