Java thread pool Learning

Source: Internet
Author: User

The top-level interface of the thread pool in Java is executor, but in a strict sense, executor is not a thread pool, but a tool for executing the thread. The real thread pool interface is executorservice.
The following figure describes the class architecture of the thread pool.

First, the executor execute method only executes a runnable task. Of course, from a certain perspective, the final implementation class also starts this task in the thread. According to the execution policy of the thread pool, the task may be executed in a new thread, a thread in the thread pool, or even the caller thread (equivalent to directly running the runnable run method ). This will be detailed later.
Executorservice adds some methods based on executor, including two core methods:

    • Future <?> Submit (runnable task)
    • <T> future <t> submit (callable <t> task)

Both methods submit tasks to the thread pool. The difference between them is that runnable has no results after execution and callable has a result after execution. This is very useful for passing statuses and results in multiple threads. In addition, they all return a future object. The future object can block the thread until the running is completed (obtain the result, if any), or cancel the task execution. Of course, it can also detect whether the task is canceled or completed.
Before there is no future, we can check whether a thread has been executed. Generally, thread. Join () is used or an endless loop and State bit is used to describe the completion of thread execution. Now we have a better way to block the thread and check whether the task has been executed or even canceled.

The features described by scheduledexecutorservice are similar to those described by Timer/timertask to solve the problems that require repeated task execution. This includes one-time execution of delayed time, periodic execution of delayed time, and periodic execution of fixed delay time. Of course, it inherits the scheduledexecutorservice of executorservice, which has all the features of executorservice.

Threadpoolexecutor is the default Implementation of executorservice. The configuration and policies are also complex. Detailed analysis will be provided in the following sections.
Scheduledthreadpoolexecutor inherits the implementation of the scheduledexecutorservice interface of threadpoolexecutor. The class implementation of periodic task scheduling will be analyzed in detail in the subsequent sections.
Here, we need to mention the completionservice interface, which is a thread pool package used to describe the sequential acquisition of execution results. It depends on a specific thread pool for scheduling, but can get the execution result according to the task execution sequence, which may improve the concurrency efficiency in some cases.

It is complicated to configure a thread pool, especially when the thread pool principle is not clear, it is very likely that the thread pool configured is not optimal, therefore, some static factories are provided in the executors class to generate some common thread pools.

    • Newsinglethreadexecutor: Create a single-threaded thread pool. This thread pool only has one thread working, which is equivalent to a single thread serial execution of all tasks. If this unique thread ends due to an exception, a new thread will replace it. This thread pool ensures that all tasks are executed in the order they are submitted.
    • Newfixedthreadpool: Create a fixed thread pool. Each time a task is submitted, a thread is created until the thread reaches the maximum size of the thread pool. The size of the thread pool remains unchanged once it reaches the maximum value. If a thread ends due to an execution exception, the thread pool will add a new thread.
    • Newcachedthreadpool: Create a cacheable thread pool. If the thread pool size exceeds the thread required to process the task, some Idle threads (not executed in 60 seconds) will be reclaimed. When the number of tasks increases, this thread pool can intelligently add new threads to process tasks. This thread pool does not limit the thread pool size. The thread pool size depends entirely on the maximum thread size that can be created by the operating system (or JVM.
    • Newscheduledthreadpool: Creates an infinite thread pool. This thread pool supports regular and periodic task execution requirements.
    • Newsinglethreadexecutor: Create a single-threaded thread pool. This thread pool supports regular and periodic task execution requirements.

The significance and principle of the above parameter configuration will be discussed in details about threadpoolexecutor.
The thread pool is a complex task scheduling tool. Therefore, it involves tasks, thread pools, and other lifecycles. We will discuss this issue in the next section.

This article is transferred from www.35java.com

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.