In layman's Java Concurrency (29): Thread pool Part 2 Executor and executors[Goto]

Source: Internet
Author: User

The top interface of the thread pool in Java is executor, but strictly speaking, executor is not a thread pool, but a tool for executing threads. The real thread pool interface is executorservice.

The following diagram provides a complete picture of the class architecture of the thread pool.

First of all, the Execute method of executor only performs a runnable task, and of course the final implementation class is also initiated in the thread from a certain angle. Depending on the execution strategy of the thread pool, this task may be executed in a new thread, or in a thread pool, or even in the caller's thread (equivalent to running the runnable Run method directly). This will be explained in detail later.

Executorservice has added several methods based on executor, of which there are two core methods:

    • Future<?> Submit (Runnable Task)
    • <T> future<t> Submit (callable<t> Task)

Both methods submit tasks to the thread pool, and the difference is that runnable has no results after execution, and there is a result after the callable has finished executing. This is useful for passing states and results in multiple threads. In addition, their same point is that they return a future object. The future object can block the thread until it finishes running (get the results, if any), cancel the task execution, and, of course, detect whether the task is canceled or executed.

Before we have a future, we detect whether a thread is executing, usually using thread.join () or a dead loop plus a status bit to describe the execution of the thread. Now there is a better way to block threads, to detect tasks that have finished executing or even cancel tasks that have not been performed.

Scheduledexecutorservice describes the same functionality as Timer/timertask, and addresses issues that require repetitive execution of tasks. This includes the one-time delay execution, the periodic execution of the delay time, and the periodic execution of the fixed delay time. Of course, inheriting Executorservice's scheduledexecutorservice has all the features of Executorservice.

Threadpoolexecutor is the default implementation of Executorservice, where the configuration and policy are more complex, with detailed analysis in later chapters.

Scheduledthreadpoolexecutor is the implementation of the Scheduledexecutorservice interface that inherits Threadpoolexecutor, and the class implementation of periodic task scheduling, which will be analyzed in detail in the later chapters.

A little mention here is the Completionservice interface, which is a thread pool wrapper that describes the sequential fetch execution results. It relies on a specific thread pool schedule, but is able to get execution results based on the order in which the tasks are executed, which in some cases may increase concurrency efficiency.

To configure a thread pool is more complex, especially if the thread pool principle is not very clear, it is likely that the thread pool configured is not superior, so there are some static factories in the executors class that generate some common thread pools.

    • Newsinglethreadexecutor: Creates a single thread pool. This thread pool has only one thread at work, which is equivalent to single-threaded serial execution of all tasks. If this unique thread ends because of an exception, a new thread will replace it. This thread pool guarantees that the order in which all tasks are executed is performed in the order in which the tasks are submitted.
    • Newfixedthreadpool: Creates a fixed-size thread pool. Each time a task is committed, a thread is created until the thread reaches the maximum size of the threads pool. Once the maximum size of the thread pool is reached, the thread pool will be replenished with a new thread if it ends up executing an exception.
    • Newcachedthreadpool: Creates a cacheable thread pool. If the size of the thread pool exceeds the thread required to process the task, then a partially idle (60 second non-performing task) thread is reclaimed, and when the number of tasks increases, the thread pool can intelligently add new threads to handle the task. This thread pool does not limit the size of the thread pool, and the thread pool size is entirely dependent on the maximum thread size that the operating system (or JVM) can create.
    • newscheduledthreadpool: Create a thread pool of unlimited size. This thread pool supports the need to schedule and periodically perform tasks.
    • Newsinglethreadscheduledexecutor: Creates a single thread pool. This thread pool supports the need to schedule and periodically perform tasks.

The meaning and principle of the above parameter configuration will be discussed in detail when explaining the threadpoolexecutor.

The thread pool is a complex task scheduling tool, so it involves life cycle issues such as tasks, thread pools, and so on, in the next section.

In layman's Java Concurrency (29): Thread pool Part 2 Executor and executors[Goto]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.