Java Foundation-executorservice (thread pool)

Source: Internet
Author: User

Today, the source code is the thread pool part, record, the source of the jdk8.

This is mainly about the process of creating a thread pool, as well as drawing a schematic picture.

Start with the execute code of the thread pool

"Source One"

Java.util.concurrent.ThreadPoolExecutor.execute (Runnable command)

/*** Executes the given task sometime in the future.     The task * May, execute in 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 has been reached, * the task was handled by the current {@codeRejectedexecutionhandler}. *     * @paramcommand the task to execute *@throwsrejectedexecutionexception at discretion of * {@codeRejectedexecutionhandler}, if the task * cannot is accepted for execution *@throwsNullPointerException If {@codecommand} is null*/     Public voidExecute (Runnable command) {if(Command = =NULL)            Throw NewNullPointerException (); /* * Proceed in 3 steps: * * 1.         If fewer than corepoolsize threads is running, try to * start a new thread with the given command as its first  * Task.         The call to Addworker atomically checks runstate and * workercount, and so prevents false alarms that would add         * Threads when it shouldn ' t, by returning false. * * 2.         If a task can be successfully queued, then we still need * to double-check whether we should has added a thread * (because existing ones died since last checking) or which * the pool shut down since entry to this Metho D. We * recheck state and if necessary rolled back the enqueuing if * stopped, or start a new thread if T         Here is none. * * 3.  If We cannot the queue task, then we try to add a new * thread.         If it fails, we know we are shut down or saturated * and so reject the task. */        intc =Ctl.get (); if(Workercountof (c) <corepoolsize) {            if(Addworker (Command,true))                return; C=Ctl.get (); }        if(IsRunning (c) &&workqueue.offer (command)) {            intRecheck =Ctl.get (); if(! isrunning (Recheck) &&Remove (command)) reject (command); Else if(Workercountof (recheck) = = 0) Addworker (NULL,false); }        Else if(!addworker (Command,false) ) reject (command); }

an int c = Ctl.get () directly to the whole, and began to turn to the beginning to see

Java.util.concurrent.ThreadPoolExecutor file Start:

Private FinalAtomicinteger ctl =NewAtomicinteger (Ctlof (RUNNING, 0)); Private Static Final intCount_bits = integer.size-3; Private Static Final intCapacity = (1 << count_bits)-1; //runstate is stored in the High-order bits    Private Static Final intRUNNING =-1 <<count_bits; Private Static Final intSHUTDOWN = 0 <<count_bits; Private Static Final intSTOP = 1 <<count_bits; Private Static Final inttidying = 2 <<count_bits; Private Static Final intTERMINATED = 3 <<count_bits; //Packing and Unpacking ctl    Private Static intRunstateof (intc) {returnC & ~capacity;} Private Static intWorkercountof (intc) {returnC &capacity;} Private Static intCtlof (intRsintWC) {returnRS | wc }

This part is a little bit difficult to understand for a long while (or the basis of byte processing is not in prison). (Friendship Hint: here understand the time please first look at the source code anti-code complement knowledge)

(by 32-bit system introduction)

Cont_bits = Integer.size-3 = 32-3 = 29

Capacity = (1 << cont_bits)-1 = 1<<29-1 = 2 of 29 square-1 = 536870911

-12 binary: 11111111111111111111111111111111

02 Binary: 0

12 binary: 1

22 binary: 10

32 Binary: 11

RUNNING =-12 binary: 1111 1111 1111 1111 1111 1111 1111 1111 << 29 = 1110 0000 0000 0000 0000 0000 0000 0000

SHUTDOWN = 02 binary: 0000 0000 0000 0000 0000 0000 0000 0000 << 29 = 0000 0000 0000 0000 0000 0000 0000 0000

STOP = 12 binary: 0000 0000 0000 0000 0000 0000 0000 << 29 = 10 0000 0000 0000 0000 0000 0000 0000

tidying = 22 binary: 0000 0000 0000 0000 0000 0000 0000 << 29 = 100 0000 0000 0000 0000 0000 0000 0000

TERMINATED = 32 binary: 1100 0000 0000 0000 0000 0000 0000 0000 << 29 = 110 0000 0000 0000 0000 0000 0000 0000

capacity=2 29 square-1 = binary: 0010 0000 0000 0000 0000 0000 0000 0000-1 = 0001 1111 1111 1111 1111 1111 1111 1111

We look at the last row, the equivalent of capacity is 0001* behind all is 1, and the state is 111*,000*,001*,010*,011*

Know this after you look at the source of the following three methods:

Ctlof (int rs, int wc) {return rs | wc;}
    private static int runstateof (int c)     {return C & ~capacity;}    private static int workercountof (int c)  {return C & capacity;} 

After this, we look at the first sentence of the code, the initial state:

The value of this atomic variable of the CTL: Ctlof (RUNNING, 0), Ctlof method: RUNNING | 0, the result is still running itself.

Runstateof is the value associated with ~capacity and operation, i.e. C & (not 0001 1111 1111 1111 1111 1111 1111 1111)

That is: C and 1110 0000 0000 0000 0000 0000 0000 0000 with, in and operation has a 0, the result is 0, ~capacity 29 is 0, in fact, is the first three-bit operation. It's a state of calculation.

Workercountof is the value and the operation of the capacity, that is, C and 0001 1111 1111 1111 1111 1111 1111 1111, and capacity the first three bits are 0, in fact, is the operation with the latter 29 bits. This is the operation of the capacity

So you understand, in fact, the state is 32 bytes, the first 3 is the state, the last 29 bits is the capacity.

In binary notation, this capacity is 0 to 2 of the 29-time Square. The first three bits are left forever, is the status bit.

Here, we can go on to the following story.

Java Foundation-executorservice (thread pool)

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.