激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - java 中ThreadPoolExecutor原理分析

java 中ThreadPoolExecutor原理分析

2020-09-03 13:42劉正陽 Java教程

這篇文章主要介紹了java 中ThreadPoolExecutor原理分析的相關資料,需要的朋友可以參考下

java 中ThreadPoolExecutor原理分析

線程池簡介

Java線程池是開發中常用的工具,當我們有異步、并行的任務要處理時,經常會用到線程池,或者在實現一個服務器時,也需要使用線程池來接收連接處理請求。

線程池使用

JDK中提供的線程池實現位于java.util.concurrent.ThreadPoolExecutor。在使用時,通常使用ExecutorService接口,它提供了submit,invokeAll,shutdown等通用的方法。

在線程池配置方面,Executors類中提供了一些靜態方法能夠提供一些常用場景的線程池,如newFixedThreadPool,newCachedThreadPool,newSingleThreadExecutor等,這些方法最終都是調用到了ThreadPoolExecutor的構造函數。

ThreadPoolExecutor的包含所有參數的構造函數是

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
   * @param corePoolSize the number of threads to keep in the pool, even
   *    if they are idle, unless {@code allowCoreThreadTimeOut} is set
   * @param maximumPoolSize the maximum number of threads to allow in the
   *    pool
   * @param keepAliveTime when the number of threads is greater than
   *    the core, this is the maximum time that excess idle threads
   *    will wait for new tasks before terminating.
   * @param unit the time unit for the {@code keepAliveTime} argument
   * @param workQueue the queue to use for holding tasks before they are
   *    executed. This queue will hold only the {@code Runnable}
   *    tasks submitted by the {@code execute} method.
   * @param threadFactory the factory to use when the executor
   *    creates a new thread
   * @param handler the handler to use when execution is blocked
   *    because the thread bounds and queue capacities are reached
  public ThreadPoolExecutor(int corePoolSize,
               int maximumPoolSize,
               long keepAliveTime,
               TimeUnit unit,
               BlockingQueue<Runnable> workQueue,
               ThreadFactory threadFactory,
               RejectedExecutionHandler handler) {
    if (corePoolSize < 0 ||
      maximumPoolSize <= 0 ||
      maximumPoolSize < corePoolSize ||
      keepAliveTime < 0)
      throw new IllegalArgumentException();
    if (workQueue == null || threadFactory == null || handler == null)
      throw new NullPointerException();
    this.corePoolSize = corePoolSize;
    this.maximumPoolSize = maximumPoolSize;
    this.workQueue = workQueue;
    this.keepAliveTime = unit.toNanos(keepAliveTime);
    this.threadFactory = threadFactory;
    this.handler = handler;
  }
  • corePoolSize設置線程池的核心線程數,當添加新任務時,如果線程池中的線程數小于corePoolSize,則不管當前是否有線程閑置,都會創建一個新的線程來執行任務。
  • maximunPoolSize是線程池中允許的最大的線程數
  • workQueue用于存放排隊的任務
  • keepAliveTime是大于corePoolSize的線程閑置的超時時間
  • handler用于在任務逸出、線程池關閉時的任務處理 ,線程池的線程增長策略為,當前線程數小于corePoolSize時,新增線程,當線程數=corePoolSize且corePoolSize時,只有在workQueue不能存放新的任務時創建新線程,超出的線程在閑置keepAliveTime后銷毀。

實現(基于JDK1.8)

ThreadPoolExecutor中保存的狀態有

當前線程池狀態, 包括RUNNING,SHUTDOWN,STOP,TIDYING,TERMINATED。

當前有效的運行線程的數量。

將這兩個狀態放到一個int變量中,前三位作為線程池狀態,后29位作為線程數量。

例如0b11100000000000000000000000000001, 表示RUNNING, 一個線程。

通過HashSet來存儲工作者集合,訪問該HashSet前必須先獲取保護狀態的mainLock:ReentrantLock

submit、execute

execute的執行方式為,首先檢查當前worker數量,如果小于corePoolSize,則嘗試add一個core Worker。線程池在維護線程數量以及狀態檢查上做了大量檢測。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public void execute(Runnable command) {
    int c = ctl.get();
    // 如果當期數量小于corePoolSize
    if (workerCountOf(c) < corePoolSize) {
      // 嘗試增加worker
      if (addWorker(command, true))
        return;
      c = ctl.get();
    }
    // 如果線程池正在運行并且成功添加到工作隊列中
    if (isRunning(c) && workQueue.offer(command)) {
      // 再次檢查狀態,如果已經關閉則執行拒絕處理
      int recheck = ctl.get();
      if (! isRunning(recheck) && remove(command))
        reject(command);
      // 如果工作線程都down了
      else if (workerCountOf(recheck) == 0)
        addWorker(null, false);
    }
    else if (!addWorker(command, false))
      reject(command);
  }

addWorker方法實現

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
private boolean addWorker(Runnable firstTask, boolean core) {
    retry:
    for (;;) {
      int c = ctl.get();
      int rs = runStateOf(c);
      // Check if queue empty only if necessary.
      if (rs >= SHUTDOWN &&
        ! (rs == SHUTDOWN &&
          firstTask == null &&
          ! workQueue.isEmpty()))
        return false;
      for (;;) {
        int wc = workerCountOf(c);
        if (wc >= CAPACITY ||
          wc >= (core ? corePoolSize : maximumPoolSize))
          return false;
        if (compareAndIncrementWorkerCount(c))
          break retry;
        c = ctl.get(); // Re-read ctl
        if (runStateOf(c) != rs)
          continue retry;
        // else CAS failed due to workerCount change; retry inner loop
      }
    }
    boolean workerStarted = false;
    boolean workerAdded = false;
    Worker w = null;
    try {
      w = new Worker(firstTask);
      final Thread t = w.thread;
      if (t != null) {
        final ReentrantLock mainLock = this.mainLock;
        mainLock.lock();
        try {
          // Recheck while holding lock.
          // Back out on ThreadFactory failure or if
          // shut down before lock acquired.
          int rs = runStateOf(ctl.get());
          if (rs < SHUTDOWN ||
            (rs == SHUTDOWN && firstTask == null)) {
            if (t.isAlive()) // precheck that t is startable
              throw new IllegalThreadStateException();
            workers.add(w);
            int s = workers.size();
            if (s > largestPoolSize)
              largestPoolSize = s;
            workerAdded = true;
          }
        } finally {
          mainLock.unlock();
        }
        if (workerAdded) {
          // 如果添加成功,則啟動該線程,執行Worker的run方法,Worker的run方法執行外部的runWorker(Worker)
          t.start();
          workerStarted = true;
        }
      }
    } finally {
      if (! workerStarted)
        addWorkerFailed(w);
    }
    return workerStarted;
  }

Worker類繼承了AbstractQueuedSynchronizer獲得了同步等待這樣的功能。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
private final class Worker
    extends AbstractQueuedSynchronizer
    implements Runnable
  {
    /**
     * This class will never be serialized, but we provide a
     * serialVersionUID to suppress a javac warning.
     */
    private static final long serialVersionUID = 6138294804551838833L;
    /** Thread this worker is running in. Null if factory fails. */
    final Thread thread;
    /** Initial task to run. Possibly null. */
    Runnable firstTask;
    /** Per-thread task counter */
    volatile long completedTasks;
    /**
     * Creates with given first task and thread from ThreadFactory.
     * @param firstTask the first task (null if none)
     */
    Worker(Runnable firstTask) {
      setState(-1); // inhibit interrupts until runWorker
      this.firstTask = firstTask;
      this.thread = getThreadFactory().newThread(this);
    }
    /** Delegates main run loop to outer runWorker */
    public void run() {
      runWorker(this);
    }
    // Lock methods
    //
    // The value 0 represents the unlocked state.
    // The value 1 represents the locked state.
    protected boolean isHeldExclusively() {
      return getState() != 0;
    }
    protected boolean tryAcquire(int unused) {
      if (compareAndSetState(0, 1)) {
        setExclusiveOwnerThread(Thread.currentThread());
        return true;
      }
      return false;
    }
    protected boolean tryRelease(int unused) {
      setExclusiveOwnerThread(null);
      setState(0);
      return true;
    }
    public void lock()    { acquire(1); }
    public boolean tryLock() { return tryAcquire(1); }
    public void unlock()   { release(1); }
    public boolean isLocked() { return isHeldExclusively(); }
    void interruptIfStarted() {
      Thread t;
      if (getState() >= 0 && (t = thread) != null && !t.isInterrupted()) {
        try {
          t.interrupt();
        } catch (SecurityException ignore) {
        }
      }
    }

runWorker(Worker)是Worker的輪詢執行邏輯,不斷地從工作隊列中獲取任務并執行它們。Worker每次執行任務前需要進行lock,防止在執行任務時被interrupt。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
final void runWorker(Worker w) {
    Thread wt = Thread.currentThread();
    Runnable task = w.firstTask;
    w.firstTask = null;
    w.unlock(); // allow interrupts
    boolean completedAbruptly = true;
    try {
      while (task != null || (task = getTask()) != null) {
        w.lock();
        // If pool is stopping, ensure thread is interrupted;
        // if not, ensure thread is not interrupted. This
        // requires a recheck in second case to deal with
        // shutdownNow race while clearing interrupt
        if ((runStateAtLeast(ctl.get(), STOP) ||
           (Thread.interrupted() &&
           runStateAtLeast(ctl.get(), STOP))) &&
          !wt.isInterrupted())
          wt.interrupt();
        try {
          beforeExecute(wt, task);
          Throwable thrown = null;
          try {
            task.run();
          } catch (RuntimeException x) {
            thrown = x; throw x;
          } catch (Error x) {
            thrown = x; throw x;
          } catch (Throwable x) {
            thrown = x; throw new Error(x);
          } finally {
            afterExecute(task, thrown);
          }
        } finally {
          task = null;
          w.completedTasks++;
          w.unlock();
        }
      }
      completedAbruptly = false;
    } finally {
      processWorkerExit(w, completedAbruptly);
    }
  }

ThreadPoolExecutor的submit方法中將Callable包裝成FutureTask后交給execute方法。

FutureTask

FutureTask繼承于Runnable和Future,FutureTask定義的幾個狀態為
NEW, 尚未執行
COMPLETING, 正在執行
NORMAL, 正常執行完成得到結果
EXCEPTIONAL, 執行拋出異常
CANCELLED, 執行被取消
INTERRUPTING,執行正在被中斷
INTERRUPTED, 已經中斷。

其中關鍵的get方法

?
1
2
3
4
5
6
public V get() throws InterruptedException, ExecutionException {
    int s = state;
    if (s <= COMPLETING)
      s = awaitDone(false, 0L);
    return report(s);
  }

先獲取當前狀態,如果還未執行完成并且正常,則進入等待結果流程。在awaitDone不斷循環獲取當前狀態,如果沒有結果,則將自己通過CAS的方式添加到等待鏈表的頭部,如果設置了超時,則LockSupport.parkNanos到指定的時間。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
static final class WaitNode {
    volatile Thread thread;
    volatile WaitNode next;
    WaitNode() { thread = Thread.currentThread(); }
  }
private int awaitDone(boolean timed, long nanos)
    throws InterruptedException {
    final long deadline = timed ? System.nanoTime() + nanos : 0L;
    WaitNode q = null;
    boolean queued = false;
    for (;;) {
      if (Thread.interrupted()) {
        removeWaiter(q);
        throw new InterruptedException();
      }
      int s = state;
      if (s > COMPLETING) {
        if (q != null)
          q.thread = null;
        return s;
      }
      else if (s == COMPLETING) // cannot time out yet
        Thread.yield();
      else if (q == null)
        q = new WaitNode();
      else if (!queued)
        queued = UNSAFE.compareAndSwapObject(this, waitersOffset,
                           q.next = waiters, q);
      else if (timed) {
        nanos = deadline - System.nanoTime();
        if (nanos <= 0L) {
          removeWaiter(q);
          return state;
        }
        LockSupport.parkNanos(this, nanos);
      }
      else
        LockSupport.park(this);
    }
  }

FutureTask的run方法是執行任務并設置結果的位置,首先判斷當前狀態是否為NEW并且將當前線程設置為執行線程,然后調用Callable的call獲取結果后設置結果修改FutureTask狀態。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public void run() {
    if (state != NEW ||
      !UNSAFE.compareAndSwapObject(this, runnerOffset,
                     null, Thread.currentThread()))
      return;
    try {
      Callable<V> c = callable;
      if (c != null && state == NEW) {
        V result;
        boolean ran;
        try {
          result = c.call();
          ran = true;
        } catch (Throwable ex) {
          result = null;
          ran = false;
          setException(ex);
        }
        if (ran)
          set(result);
      }
    } finally {
      // runner must be non-null until state is settled to
      // prevent concurrent calls to run()
      runner = null;
      // state must be re-read after nulling runner to prevent
      // leaked interrupts
      int s = state;
      if (s >= INTERRUPTING)
        handlePossibleCancellationInterrupt(s);
    }
  }

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

原文鏈接:https://liuzhengyang.github.io/2017/03/27/threadpoolexecutor/

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产精品91在线 | 成人在线a| 毛片网站视频 | 黄色av一区二区三区 | 亚洲一区二区在线免费 | 7777欧美| 亚洲国产一区二区三区 | 中文字幕在线网 | 成人午夜免费在线视频 | 国产精品99一区二区 | 国产日韩一区二区三区在线观看 | 无码专区aaaaaa免费视频 | 精品一区二区三区免费毛片爱 | 999久久久精品 | h色视频网站 | 国产91久久精品一区二区 | 久久骚 | 国产黄色免费网站 | 一级黄色影片在线观看 | 国产精品视频一区二区三区四 | 青草av.久久免费一区 | 欧美日韩国产一区二区三区在线观看 | 日本欧美一区 | 久草导航 | 国产精品片www48888 | 性欧美极品xxxx欧美一区二区 | 成人黄视频在线观看 | 一区在线看 | 国产一区二区二 | 欧美亚洲一级 | 91看片免费看 | 久久精片| 亚洲二区免费 | 久久九九热re6这里有精品 | 新久草视频| 狠狠干视频网站 | 久久av电影院 | 久久99精品久久久久久236 | 中文字幕在线免费播放 | 一级一级一级一级毛片 | 日韩精品一区二区三区中文 |