Worker worker thread of ThreadPoolExecutor

The Worker worker thread of ThreadPoolExecutor mainly checks the status of the thread pool and the number of worker threads. Add a worker thread and start the worker thread.

Verify the thread pool status and the number of worker threads code display

  • Two points need to be clarified. First, the outer loop checks the status of the thread pool.

  • The inner loop checks the number of worker threads.

  • retry is marked to give the outer loop a name. It is convenient for the innermost for loop to jump out of the loop.

  • int c = ctl.get(); get ctl

  • int rs = runStateOf©; Get the value of the high three digits of ctl

  • if (rs >= SHUTDOWN &&
    ! (rs == SHUTDOWN &&
    firstTask == null &&
    ! workQueue.isEmpty()))
    return false; This part is the judgment of thread pool status. The condition for adding is that the thread pool status is SHUTDOWN. And there are tasks in the blocking queue, and the number of worker threads is 0. Add a worker thread to process tasks in the blocking queue.
    If the above ideal conditions are not met, then false.

  • The next step is to judge the number of worker threads.

  • int wc = workerCountOf©; Get the value of the lower 29 bits of ctl, that is, get the number of worker threads.

  • If the number of worker threads is greater than the maximum value, it will not be added. returns false. Another judgment is to judge whether the current thread is core or non-core. The core is judged based on corePoolSize; the non-core is judged based on corePoolSize.

  • compareAndIncrementWorkerCount©. Here, CAS is used to add 1 to ctl;

  • If the above +1 fails. Re-acquire the value of ctl. Then compare the state of the thread with the previous one. If there is a difference, it means that there is a change, jump out of the outer for loop once.

insert image description here

Add a worker thread and start the source code analysis of the worker thread

  • Three variables are declared.
  • The worker thread starts no. false. not started
  • Worker threads added no. false. Not added.
  • The worker thread defaults to null.
  • Method analysis inside try.
  • w = new Worker(firstTask). Build worker threads and pass tasks to them.
  • final Thread t = w.thread; Get the thread object of word built above.
  • t != null. Judge the Thred object. In the new word, the Thred object is constructed through the Thred factory. Then hand it over to the worker object. If it is null, it proves that there is a problem with the thread factory.
  • final ReentrantLock mainLock = this.mainLock; Locking ensures the safety of using the workers object and adding threads to the largestPoolSize variable.
  • int rs = runStateOf(ctl.get()); Get the status of the thread pool again.
  • if (rs < SHUTDOWN ||
    (rs == SHUTDOWN && firstTask == null))
    is less than SHUTDOWN This flag indicates that the state of the thread pool is RUNNING and the state is normal. And firstTask is null to add a non-core worker thread to process tasks in the blocking queue.
  • t. isAlive(). Here is the verification that worker threads can be added but threads cannot be started. If it is started, an IllegalThreadStateException will be thrown.
  • workers.add(w); workers here are private final HashSet workers = new HashSet();
  • int s = workers.size(); Get the number of threads of the thread.
  • if (s > largestPoolSize). The current number of threads is greater than the maximum number of threads to update the maximum number of threads.
  • if (workerAdded) { t.start(); workerStarted = true; } Thread added successfully starts worker thread



  • if (workerAdded) { t.start(); workerStarted = true; } If adding a worker thread fails, handle the failed worker thread.



insert image description here

Operations that fail to add threads

  • final ReentrantLock mainLock = this.mainLock; It involves locking when operating works.
  • tryTerminate(); Add thread failure state transition operation. . . . . . . . . . . . .

insert image description here

Guess you like

Origin blog.csdn.net/weixin_50503886/article/details/131562268