开发者社区> 问答> 正文

解释ExecutorService接口。

ExecutorService接口作为Java中一个常用到的接口,怎么实现的?

展开
收起
YDYK 2020-04-24 16:35:43 715 0
2 条回答
写回答
取消 提交回答
  • 有点尴尬唉 你要寻找的东西已经被吃掉啦!

    ExecutorService接口是Executor接口的子接口,并添加了管理生命周期的功能。 请考虑以下示例代码:

    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.TimeUnit;

    public class TestThread {
    public static void main(final String[] arguments) throws InterruptedException {
    ExecutorService e = Executors.newSingleThreadExecutor();

      try {  
         e.submit(new Thread());  
         System.out.println("Shutdown executor");  
         e.shutdown();  
         e.awaitTermination(5, TimeUnit.SECONDS);  
      } catch (InterruptedException ex) {  
         System.err.println("tasks interrupted");  
      } finally {  
    
         if (!e.isTerminated()) {  
            System.err.println("cancel non-finished tasks");  
         }  
         e.shutdownNow();  
         System.out.println("shutdown finished");  
      }  
    

    }

    static class Task implements Runnable {

      public void run() {  
    
         try {  
            Long duration = (long) (Math.random() * 20);  
            System.out.println("Running Task!");  
            TimeUnit.SECONDS.sleep(duration);  
         } catch (InterruptedException ex) {  
            ex.printStackTrace();  
         }  
      }  
    

    }
    } Java

    执行上面示例代码,得到以下结果:

    Shutdown executor shutdown finished

    2020-04-24 16:37:14
    赞同 展开评论 打赏
  •       
    import java.util.concurrent.ExecutorService;  
    import java.util.concurrent.Executors;  
    import java.util.concurrent.TimeUnit;  
      
    public class TestThread {  
       public static void main(final String[] arguments) throws InterruptedException {  
          ExecutorService e = Executors.newSingleThreadExecutor();  
      
          try {  
             e.submit(new Thread());  
             System.out.println("Shutdown executor");  
             e.shutdown();  
             e.awaitTermination(5, TimeUnit.SECONDS);  
          } catch (InterruptedException ex) {  
             System.err.println("tasks interrupted");  
          } finally {  
      
             if (!e.isTerminated()) {  
                System.err.println("cancel non-finished tasks");  
             }  
             e.shutdownNow();  
             System.out.println("shutdown finished");  
          }  
       }  
      
       static class Task implements Runnable {  
            
          public void run() {  
               
             try {  
                Long duration = (long) (Math.random() * 20);  
                System.out.println("Running Task!");  
                TimeUnit.SECONDS.sleep(duration);  
             } catch (InterruptedException ex) {  
                ex.printStackTrace();  
             }  
          }  
       }         
    } 
    
    2020-04-24 16:36:11
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载