当前位置:   article > 正文

Java中的线程池ThreadPoolExecutor_java threadpooltaskexecutor

java threadpooltaskexecutor

Java中创建线程的方法有四种,分别为继承Thread类,实现Runnable接口,实现Callable接口和通过线程池。在实际开发中,我们最常用的就是线程池的方法,因为利用线程池我们可以重复利用线程,减少每次创建线程所带来的额外开销;同时使用线程池也方便我们维护管理线程信息;此外,线程池的存在也可以帮助我们避免出现因为代码bug导致的无限创建线程导致系统资源耗尽等问题。

Java中通过使用ThreadPoolExecutor来创建管理线程池,而Spring框架中使用ThreadPoolTaskExecutor管理线程池,这是两个不同的组件,但spring中的ThreadPoolTaskExecutor的核心处理逻辑还是Java中的ThreadPoolExecutor,只是对其进行封装使得其可以成为Bean,交由Spring Context进行管理。

接下来,让我们来了解一下Java中的线程池ThreadPoolExecutor。

1.ThreadPoolExecutor简介

ThreadPoolExecutor类继承AbstractExecutorService类,实现ExecutorService,ExecutorService

接口,下面让我们先看一下这两个接口都定义了什么方法:

  1. public interface Executor {
  2. /**
  3. * Executes the given command at some time in the future. The command
  4. * may execute in a new thread, in a pooled thread, or in the calling
  5. * thread, at the discretion of the {@code Executor} implementation.
  6. *
  7. * @param command the runnable task
  8. * @throws RejectedExecutionException if this task cannot be
  9. * accepted for execution
  10. * @throws NullPointerException if command is null
  11. */
  12. void execute(Runnable command);
  13. }
  1. public interface ExecutorService extends Executor {
  2. void shutdown();
  3. List<Runnable> shutdownNow();
  4. boolean isShutdown();
  5. boolean isTerminated();
  6. boolean awaitTermination(long timeout, TimeUnit unit)
  7. throws InterruptedException;
  8. <T> Future<T> submit(Callable<T> task);
  9. <T> Future<T> submit(Runnable task, T result);
  10. Future<?> submit(Runnable task);
  11. <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
  12. throws InterruptedException;
  13. <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks,
  14. long timeout, TimeUnit unit)
  15. throws InterruptedException;
  16. <T> T invokeAny(Collection<? extends Callable<T>> tasks)
  17. throws InterruptedException, ExecutionException;
  18. <T> T invokeAny(Collection<? extends Callable<T>> tasks,
  19. long timeout, TimeUnit unit)
  20. throws InterruptedException, ExecutionException, TimeoutException;
  21. }

可以看出,Executor接口只定义了一个提交线程的方法,而ExecutorSrevice方法则基础Executor并扩展了它的功能,ExecutorSrevice中的方法看名字大多数也可以看出来,在此我们不做赘述,而ExecutorSrevice中的方法,AbstractExecutorService类都进行了默认的实现,下面我们来看一下AbstractExecutorService这个类中部分方法的实现:

  1. public abstract class AbstractExecutorService implements ExecutorService {
  2. protected <T> RunnableFuture<T> newTaskFor(Runnable runnable, T value) {
  3. return new FutureTask<T>(runnable, value);
  4. }
  5. protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) {
  6. return new FutureTask<T>(callable);
  7. }
  8. public Future<?> submit(Runnable task) {
  9. if (task == null) throw new NullPointerException();
  10. RunnableFuture<Void> ftask = newTaskFor(task, null);
  11. execute(ftask);
  12. return ftask;
  13. }
  14. public <T> Future<T> submit(Runnable task, T result) {
  15. if (task == null) throw new NullPointerException();
  16. RunnableFuture<T> ftask = newTaskFor(task, result);
  17. execute(ftask);
  18. return ftask;
  19. }
  20. public <T> Future<T> submit(Callable<T> task) {
  21. if
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/天景科技苑/article/detail/1008911
推荐阅读
相关标签
  

闽ICP备14008679号