Thread pool in JDK1.8

Source: Internet
Author: User

The above code has been in use, the interview is often asked, but never delve into, do not know what the thread pool is going on, today look at the source code, a probe into its

The main control state of a thread pool is a CTL, which is an atomic integer that contains two concept fields:

    • Workercount: Number of valid threads
    • Runstate: The status of the thread pool

In order to include these two fields in an integer value, we limit workercount up to 2 of the 29 square minus 1

There are several values for runstate:

    • RUNNING: Accepting new tasks and working with tasks in the queue
    • SHUTDOWN: Do not accept new tasks, continue processing tasks in queue
    • STOP: Do not accept new tasks, do not process tasks in the queue, and interrupt tasks that are being processed
    • Tidying: All tasks are finished, Workercount is 0, the state is converted by calling the terminated () method
    • The terminated:terminated () method has been completed

Transitions between states such as:

RUNNING-SHUTDOWN

Call the Shutdown () method, or implicitly call the Finalize () method

(RUNNING or SHUTDOWN), STOP

Call the Shoutdownnow () method

SHUTDOWN-Tidying

When both the queue and the pool are empty

STOP-Tidying

When the pool is empty

Tidying-TERMINATED

When the terminated () method call is complete

Integer.size=31

integer.size-3 = 29

So, count_bits = 29

High 3-bit storage runstate

Next, look at the most complicated construction method.

Detailed parameters

    • Corepoolsize: Number of threads that remain in the pool (PS: Even if they are idle)
    • Maximumpoolsize: Maximum number of threads allowed in the pool
    • KeepAliveTime: The maximum lifetime of the thread exceeded when the number of threads exceeds the number of core threads
    • Units of the Unit:keepalivetime
    • WorkQueue: Maintaining queues for pending tasks
    • Threadfactory: Creating a thread Factory

1. If the number of threads running is less than the number of core threads, create a new thread to run the task

2. If the work queue is not full, put it in the work queue

3. If the work queue is full (PS:workQueue.offer (command) returns false), then create a new thread

4, if the number of threads has reached the maximum number of threads reject (command)

In the preceding Execute method, there are 3 calls to the Addwork () method

First, if the current number of active threads is less than the number of core threads, then the number of threads at the boundary is the number of core threads

Second, if the current number of active threads exceeds the number of core threads and the new task is placed in the queue, the number of valid threads is 0, then a new thread is created

Third, if the current number of active threads exceeds the number of core threads, and the queue is full, and the number of valid threads is less than the maximum number of threads, this is called to create a new thread.

The current active thread is in works, and works has a worker object, and then the worker

In the previous step, the thread's start () method, when the thread runs, executes the run () method, and the worker inherits the Runnable and re-run () method, and the run () method calls the external Runworker () method, so The Runworker () method is called when the newly created thread in the threads pool runs

The Runworker () method loops the task out of the queue and executes it. That is, all the threads in the pool are created with new tasks, then the new tasks are executed, and the tasks are pulled out of the queue and executed

Next, look at the differences between several thread pools that are common in executors

Can see

Newsinglethreadexecutor: The number of core threads and the maximum number of threads is 1, and the queue is Linkedblockingqueue

Newfixedthreadpool: The number of core threads equals the maximum number of threads, the idle thread lifetime that exceeds the number of core threads is 0, and the queue is Linkedblockingqueue

Newcachedthreadpool: The number of core threads is 0, the maximum number of threads is integer.max_value, the idle thread lifetime is 1min, the queue is Synchronousqueue

Thread pool in JDK1.8

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.