美文网首页
线程池源码-1

线程池源码-1

作者: 哈哈11122 | 来源:发表于2022-07-04 11:24 被阅读0次

Executor、ExecutorService、ThreadPoolExecutor、Executors
ExecutorService继承了Executor,

Executor接口

先来看一下Executor接口,只定义了一个execute方法,用来执行任务(Runnable)。

public interface Executor {

    /**
     * Executes the given command at some time in the future.  The command
     * may execute in a new thread, in a pooled thread, or in the calling
     * thread, at the discretion of the {@code Executor} implementation.
     *
     * @param command the runnable task
     * @throws RejectedExecutionException if this task cannot be
     * accepted for execution
     * @throws NullPointerException if command is null
     */
    void execute(Runnable command);
}

ExecutorService接口

ExecutorService继承了Executor,主要的方法是submit(task),执行任务并返回一个Future对象用于获取执行结果。

public interface ExecutorService extends Executor {
    /**
     * Submits a value-returning task for execution and returns a
     * Future representing the pending results of the task. The
     * Future's {@code get} method will return the task's result upon
     * successful completion.
     *
     * <p>
     * If you would like to immediately block waiting
     * for a task, you can use constructions of the form
     * {@code result = exec.submit(aCallable).get();}
     *
     * <p>Note: The {@link Executors} class includes a set of methods
     * that can convert some other common closure-like objects,
     * for example, {@link java.security.PrivilegedAction} to
     * {@link Callable} form so they can be submitted.
     *
     * @param task the task to submit
     * @param <T> the type of the task's result
     * @return a Future representing pending completion of the task
     * @throws RejectedExecutionException if the task cannot be
     *         scheduled for execution
     * @throws NullPointerException if the task is null
     */
    <T> Future<T> submit(Callable<T> task);

    /**
     * Submits a Runnable task for execution and returns a Future
     * representing that task. The Future's {@code get} method will
     * return {@code null} upon <em>successful</em> completion.
     *
     * @param task the task to submit
     * @return a Future representing pending completion of the task
     * @throws RejectedExecutionException if the task cannot be
     *         scheduled for execution
     * @throws NullPointerException if the task is null
     */
    Future<?> submit(Runnable task);
}

ThreadPoolExecutor实现类

ThreadPoolExecutor间接地实现了ExecutorService接口。ThreadPoolExecutor提供一个线程池,执行ExecutorService .submit(task)方法时,使用线程池里的线程执行任务。
在Executors类里是这样新建ThreadPoolExecutor的:

public class Executors {
/**
     * Creates a thread pool that reuses a fixed number of threads
     * operating off a shared unbounded queue.  At any point, at most
     * {@code nThreads} threads will be active processing tasks.
     * If additional tasks are submitted when all threads are active,
     * they will wait in the queue until a thread is available.
     * If any thread terminates due to a failure during execution
     * prior to shutdown, a new one will take its place if needed to
     * execute subsequent tasks.  The threads in the pool will exist
     * until it is explicitly {@link ExecutorService#shutdown shutdown}.
     *
     * @param nThreads the number of threads in the pool
     * @return the newly created thread pool
     * @throws IllegalArgumentException if {@code nThreads <= 0}
     */
    public static ExecutorService newFixedThreadPool(int nThreads) {
        return new ThreadPoolExecutor(nThreads, nThreads,
                                      0L, TimeUnit.MILLISECONDS,
                                      new LinkedBlockingQueue<Runnable>());
    }
...
}

测试代码

Executors.newFixedThreadPool()新建了一个ThreadPoolExecutor,ThreadPoolExecutor实现了ExecutorService接口的submit(task)。

// 创建一个固定大小的线程池:
        ExecutorService es = Executors.newFixedThreadPool(4);
        for (int i = 0; i < 6; i++) {
            es.submit(new Task("" + i));
        }
        // 关闭线程池:
        es.shutdown();

相关文章

  • Java线程池

    线程池源码 1、一般我们会直接使用Excutors创建线程池: 2、线程池的创建都会交给ThreadPoolExe...

  • 线程池解析第二章-线程池源码问题总结

    线程池解析第一章-源码解析线程池解析第二章-线程池源码问题总结 上篇文章分析了关于线程池在源码层面对提交任务的处理...

  • 线程池解析第一章-源码解析

    线程池解析第一章-源码解析线程池解析第二章-线程池源码问题总结 线程池基本介绍 为什么要使用线程池 对于系统和服务...

  • 线程池源码-1

    Executor、ExecutorService、ThreadPoolExecutor、ExecutorsExec...

  • 线程池ThreadPoolExecutor源码解析

    本文主要内容 什么是线程池 线程池的使用 线程池的原理 线程池中的位运算 源码解析 什么是线程池 如果频繁创建线程...

  • 线程池源码

    在Android-27查看源码 线程池的创建,查看Executors的源码: 1、newFixedThreadPo...

  • java线程池深入理解

    线程池经常被面试问到,引起了我的重视,于是花了几个小时看了线程池的源码,做了如下总结 一、线程池主要配置 1.核心...

  • 并发编程的艺术-线程池源码解析

    并发编程的艺术-线程池源码解析 线程池的作用: 1,降低资源消耗。通过重复利用已创建的线程降低线程创建和销毁造成的...

  • 分析jdk-1.8-ForkJoinPool实现原理(上)

    Java并发编程源码分析系列: 分析Java线程池的创建 分析Java线程池执行原理 分析Java线程池Calla...

  • 分析jdk-1.8-ForkJoinPool实现原理(下)

    Java并发编程源码分析系列: 分析Java线程池的创建 分析Java线程池执行原理 分析Java线程池Calla...

网友评论

      本文标题:线程池源码-1

      本文链接:https://www.haomeiwen.com/subject/pzwpvrtx.html