In layman Java Concurrency (34): Thread pool Part 7 thread pool implementation and principle (2) [Go]

Source: Internet
Author: User

Thread pool Task Execution flow

We start with an API to contact executor how the task queue is handled.

Java.util.concurrent.Executor.execute (Runnable)

Executes the given task sometime in the future. The task may execute with a new thread or in an existing pooled thread. If The task cannot is submitted for execution, either because this executor have been shutdown or because its capacity have been reached, the task is handled by the current Rejectedexecutionhandler.

All task executions in the thread pool are dependent on this interface. This passage has the following meanings:

    1. A task may be executed at some point in the future and may not be executed immediately. Why are there two "possibilities" here? Keep looking down.
    2. The task may be executed in a new thread or in a thread that exists in the thread pool.
    3. There are two reasons why a task cannot be committed: The thread pool has been closed or the thread pool has reached the capacity limit.
    4. All failed tasks will be processed by the "current" task Reject policy Rejectedexecutionhandler.

Answer the above two "possible". The task may be executed, and the impossible situation is that the above scenario is 3; it may not be done immediately, because the task may also be queued in the queue, so it is still waiting for the allocation thread to execute. Knowing the literal question, let's look at the concrete implementation.

public void Execute (Runnable command) {
if (command = = null)
throw new NullPointerException ();
if (poolsize >= corepoolsize | |!addifundercorepoolsize (command)) {
if (runstate = = RUNNING && workqueue.offer (command)) {
if (runstate! = RUNNING | | poolsize = = 0)
ensurequeuedtaskhandled (command);
}
else if (!addifundermaximumpoolsize (command))
Reject (command); is shutdown or saturated
}
}

This piece of code looks very simple, in fact, this is the most important part of the thread pool, if you can fully understand this, the thread pool is quite easy. The entire execution process is this:

    1. If the task command is empty, a null pointer exception is thrown and returned. Otherwise proceed to 2.
    2. 4 If the current thread pool size is greater than or equal to the core thread pool size. Otherwise proceed to 3.
    3. Create a new Work queue (thread, refer to previous section), successfully returned directly, failed to 4.
    4. If the thread pool is running and the task joins the thread pool queue successfully, proceed to 5, otherwise 7.
    5. If the thread pool is closed or the thread pool size is 0, make 6, otherwise return directly.
    6. If the thread pool is closed, the deny policy is returned, otherwise a new thread is started to perform the task and return.
    7. If the thread pool size is not greater than the maximum number of thread pools, a new thread is started to execute, otherwise the deny policy ends.

is the text description step not simple enough? The following figure describes this procedure in detail.

To be honest, this figure is more difficult to understand than the above steps, so where to start?

The entrance to the process is simple, we are going to execute a task (Runnable command), so where is the end point or what?

According to the diagram on the left, we know that there may be several exits:

(1) P1, P7 in the figure, we can see from this path that the task is simply added to the task queue (offer command);

(2) The P3 in the figure, this path does not join the task queue, but initiates a new worker thread (worker) to mop up the operation, the user processes the empty task queue;

(3) The P4 in the figure, this path does not join the task queue, but started a new worker thread (worker), and the first task on the job site is the current task;

(4) P5, P6 in the figure, this path does not join the task queue, and does not start the worker thread, just throw to the task rejection policy. P2 is a task that joins the task queue but is removed from the task queue because the thread pool is closed and is thrown to a deny policy.

If the above explanation is not clear, you can study the following two sections of code:

Java.util.concurrent.ThreadPoolExecutor.addIfUnderCorePoolSize (Runnable)
Java.util.concurrent.ThreadPoolExecutor.addIfUnderMaximumPoolSize (Runnable)
Java.util.concurrent.ThreadPoolExecutor.ensureQueuedTaskHandled (Runnable)

So when does a task get executed immediately?

Thread pool running state, if the thread pool size is smaller than the core thread pool size or the thread pool is full (the task queue is full) and the thread pool size is less than the maximum thread pool size (at this point the thread pool size is larger than the core thread pool size), the program is described as:

Runstate = = RUNNING && (poolsize < Corepoolsize | | poolsize < maxnumpoolsize && Workqueue.isfull ())

The above condition is a condition in which a task can be executed immediately.

With the basics of execute, let's look at the implementation of several of the submit methods in Executorservice.

Public future<?> Submit (Runnable Task) {
if (task = = null) throw new NullPointerException ();
runnablefuture<object> ftask = newtaskfor (task, NULL);
Execute (ftask);
return ftask;
}

Public <T> future<t> Submit (Runnable task, T result) {
if (task = = null) throw new NullPointerException ();
runnablefuture<t> ftask = newtaskfor (task, result);
Execute (ftask);
return ftask;
}

Public <T> future<t> Submit (callable<t> Task) {
if (task = = null) throw new NullPointerException ();
runnablefuture<t> ftask = newtaskfor (Task);
Execute (ftask);
return ftask;
}

It's simple, isn't it? For a line pool said the complex place is also in the Execute method execution process. In the next section we will discuss how to get the results of the task execution, that is, the use and principle of the future class.

Inside.java.concurrency 34.thread Pool.part7_threadpoolexecutor_executeView more documents from Xylz.

In layman Java Concurrency (34): Thread pool Part 7 thread pool implementation and principle (2) [Go]

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.