温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Java Executor执行器任务提交方式

发布时间:2025-06-06 13:27:19 来源:亿速云 阅读:98 作者:小樊 栏目:编程语言

在Java中,Executor框架提供了一种将任务提交给线程池执行的方式。以下是使用Executor执行器提交任务的几种常见方法:

  1. execute(Runnable command): 这是最基本的方法,用于提交不需要返回结果的任务。如果任务执行过程中发生异常,它将被捕获并传递给未捕获异常处理器。
ExecutorService executor = Executors.newFixedThreadPool(4);
executor.execute(new Runnable() {
    @Override
    public void run() {
        System.out.println("Task is running on thread " + Thread.currentThread().getName());
    }
});
  1. submit(Callable task): 用于提交需要返回结果的任务。它返回一个Future对象,可以用来检查任务是否完成,并获取任务的返回值。
ExecutorService executor = Executors.newFixedThreadPool(4);
Future<Integer> future = executor.submit(new Callable<Integer>() {
    @Override
    public Integer call() throws Exception {
        System.out.println("Task is running on thread " + Thread.currentThread().getName());
        return 42;
    }
});

try {
    // 获取任务结果,如果任务未完成,get() 方法将阻塞直到完成
    Integer result = future.get();
    System.out.println("Task returned: " + result);
} catch (InterruptedException | ExecutionException e) {
    e.printStackTrace();
}
  1. submit(Runnable task, T result): 这个方法允许你在提交任务时指定一个结果,当任务完成时,这个结果将被设置到Future对象中。
ExecutorService executor = Executors.newFixedThreadPool(4);
Future<Integer> future = executor.submit(new Runnable() {
    @Override
    public void run() {
        System.out.println("Task is running on thread " + Thread.currentThread().getName());
    }
}, 42);

try {
    // 获取任务结果,如果任务未完成,get() 方法将阻塞直到完成
    Integer result = future.get();
    System.out.println("Task returned: " + result);
} catch (InterruptedException | ExecutionException e) {
    e.printStackTrace();
}
  1. invokeAll(Collection<? extends Callable> tasks): 提交一组任务,并等待所有任务完成。返回一个Future列表,每个Future对应一个任务的执行结果。
ExecutorService executor = Executors.newFixedThreadPool(4);
List<Callable<Integer>> tasks = new ArrayList<>();
tasks.add(new Callable<Integer>() {
    @Override
    public Integer call() throws Exception {
        System.out.println("Task 1 is running on thread " + Thread.currentThread().getName());
        return 42;
    }
});
tasks.add(new Callable<Integer>() {
    @Override
    public Integer call() throws Exception {
        System.out.println("Task 2 is running on thread " + Thread.currentThread().getName());
        return 24;
    }
});

try {
    List<Future<Integer>> futures = executor.invokeAll(tasks);
    for (Future<Integer> future : futures) {
        System.out.println("Task returned: " + future.get());
    }
} catch (InterruptedException | ExecutionException e) {
    e.printStackTrace();
}
  1. invokeAny(Collection<? extends Callable> tasks): 提交一组任务,并返回第一个成功完成的任务的结果。如果所有任务都失败,则抛出ExecutionException。
ExecutorService executor = Executors.newFixedThreadPool(4);
List<Callable<Integer>> tasks = new ArrayList<>();
tasks.add(new Callable<Integer>() {
    @Override
    public Integer call() throws Exception {
        System.out.println("Task 1 is running on thread " + Thread.currentThread().getName());
        return 42;
    }
});
tasks.add(new Callable<Integer>() {
    @Override
    public Integer call() throws Exception {
        System.out.println("Task 2 is running on thread " + Thread.currentThread().getName());
        throw new RuntimeException("Task 2 failed");
    }
});

try {
    Integer result = executor.invokeAny(tasks);
    System.out.println("Task returned: " + result);
} catch (InterruptedException | ExecutionException e) {
    e.printStackTrace();
}

在使用完ExecutorService后,应该调用shutdown()shutdownNow()方法来关闭执行器,以避免资源泄漏。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI