线程池中 submit()和 execute()方法有什么区别

导读:本篇文章讲解 线程池中 submit()和 execute()方法有什么区别,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

submit()和 execute()方法的区别

  • execute 方法执行完成后没有返回值,而submit方法执行后有返回值
  • 方法所在的类不同,
    execute 方法:java.util.concurrent.Executor;
    submit方法:java.util.concurrent.ExecutorService
  • 所需要的参数不同,
    execute : java.util.concurrent.Executor#execute(java.lang.Runnable)
    submit: java.util.concurrent.ExecutorService#submit(java.lang.Runnable, T),
                   java.util.concurrent.ExecutorService#submit(java.lang.Runnable)
                   java.util.concurrent.ExecutorService#submit(java.util.concurrent.Callable<T>)

代码示例

  • 无返回值的 java.util.concurrent.Executor#execute(java.lang.Runnable)
   @Test
   public void testExecute() {
       executorService = Executors.newCachedThreadPool();
       for (int i = 0; i < 5; i++) {
           final int index = i;
           executorService.execute(() -> System.out.println(Thread.currentThread().getName() + "-" + index));
       }
       executorService.shutdown();
   }
  • 有返回值的 java.util.concurrent.ExecutorService#submit(java.lang.Runnable)
 
    @Test
    public void testSubmit() {
        List<Future<String>> res = new ArrayList<>();
        ExecutorService executorService = Executors.newCachedThreadPool();
        for (int i = 0; i < 10; i++) {
            final int ind = i;
            Future<String> submit = executorService.submit(() -> {
                System.out.println(ind + " --> return String....");
                return "结果:" + ind;
            });
            res.add(submit);
        }

        res.forEach(r -> {
            try {
                System.out.println("res->" + r.get());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        });
    }
  • 有返回值的 java.util.concurrent.ExecutorService#submit(java.lang.Runnable, T)

    @Test
    public void testSubmit2() {
        List<Future<String>> res = new ArrayList<>();
        ExecutorService executorService = Executors.newCachedThreadPool();
        for (int i = 0; i < 10; i++) {
            final int ind = i;
            String temp = "结果2:" + ind;
            Future<String> submit = executorService.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println(ind + " --> return String....");
                }
            }, temp);
            res.add(submit);
        }

        res.forEach(r -> {
            try {
                System.out.println("res->" + r.get());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        });
    }
  • 有返回值的 java.util.concurrent.ExecutorService#submit(java.util.concurrent.Callable<T>)
@Test
   public void testCallable() throws InterruptedException {
       List<Future<String>> res = new ArrayList<>();
       ExecutorService executorService = Executors.newCachedThreadPool();
       for (int i = 0; i < 10; i++) {
           final int ind = i;
           Future<String> submit = executorService.submit(new Callable<String>() {
               @Override
               public String call() throws Exception {
                   System.out.println(ind + " --> return String....");
                   return "结果:" + ind;
               }
           });
           res.add(submit);
       }

       res.forEach(r -> {
           try {
               System.out.println("res->" + r.get());
           } catch (InterruptedException e) {
               e.printStackTrace();
           } catch (ExecutionException e) {
               e.printStackTrace();
           }
       });

       Thread.sleep(40);
   }

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/15409.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!