博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
了解一下CompletionService的submit(Runnable task, V result)方法
阅读量:222 次
发布时间:2019-02-28

本文共 787 字,大约阅读时间需要 2 分钟。

示例demo:

public class CompletionServiceDemo {
public static void main(String[] args) throws ExecutionException, InterruptedException {
ExecutorService pool = Executors.newFixedThreadPool(3); ExecutorCompletionService
completionService = new ExecutorCompletionService(pool); Future
future = completionService.submit(new Runnable() {
@Override public void run() {
try {
TimeUnit.SECONDS.sleep(5); } catch (InterruptedException e) {
e.printStackTrace(); } } }, "我是默认值"); System.out.println(future.get()); pool.shutdown(); }}

运行结果:

在这里插入图片描述

总结

从运行结果可以看出,这个方法实际作用就是为Runnable类型的任务设置一个默认值返回。

转载地址:http://ljjp.baihongyu.com/

你可能感兴趣的文章