|
| 1 | +@startuml thread-pool-executor-demo |
| 2 | + |
| 3 | +participant ThreadPoolExecutorDemo |
| 4 | +participant ThreadPoolExecutor |
| 5 | +participant Worker |
| 6 | +participant Thread |
| 7 | +participant Executors |
| 8 | +participant DefaultThreadFactory |
| 9 | + |
| 10 | +ThreadPoolExecutorDemo -> ThreadPoolExecutor : new ThreadPoolExecutor(); |
| 11 | +activate ThreadPoolExecutor |
| 12 | + ThreadPoolExecutor -> ThreadPoolExecutor : ctl = new AtomicInteger(ctlOf(RUNNING, 0)); |
| 13 | + activate ThreadPoolExecutor |
| 14 | + deactivate ThreadPoolExecutor |
| 15 | + ThreadPoolExecutor -> Executors : Executors.defaultThreadFactory() |
| 16 | + ThreadPoolExecutor <-- DefaultThreadFactory : java.util.concurrent.Executors.DefaultThreadFactory; |
| 17 | + ThreadPoolExecutorDemo <-- ThreadPoolExecutor : ThreadPoolExecutor instance |
| 18 | +deactivate ThreadPoolExecutor |
| 19 | + |
| 20 | +... |
| 21 | + |
| 22 | +ThreadPoolExecutorDemo -> ThreadPoolExecutor : allowCoreThreadTimeOut(true) |
| 23 | + |
| 24 | +... |
| 25 | + |
| 26 | +ThreadPoolExecutorDemo -> ThreadPoolExecutor: execute(command); |
| 27 | +activate ThreadPoolExecutor |
| 28 | + ThreadPoolExecutor -> ThreadPoolExecutor : addWorker(command, true) |
| 29 | + activate ThreadPoolExecutor |
| 30 | + ThreadPoolExecutor -> Worker: new Worker(command); |
| 31 | + activate Worker |
| 32 | + Worker -> DefaultThreadFactory : newThread(this); |
| 33 | + Worker <-- DefaultThreadFactory : Thread instance |
| 34 | + ThreadPoolExecutor <-- Worker : Worker instance |
| 35 | + deactivate Worker |
| 36 | + ThreadPoolExecutor -> ThreadPoolExecutor : workers.add(worker) |
| 37 | + ThreadPoolExecutor -> Thread : Thread start() |
| 38 | + deactivate ThreadPoolExecutor |
| 39 | +deactivate ThreadPoolExecutor |
| 40 | + |
| 41 | +ThreadPoolExecutorDemo -> ThreadPoolExecutor : shutdown() |
| 42 | +activate ThreadPoolExecutor |
| 43 | + ThreadPoolExecutor -> ThreadPoolExecutor : advanceRunState(SHUTDOWN); |
| 44 | +deactivate ThreadPoolExecutor |
| 45 | + |
| 46 | +... worker thread start ... |
| 47 | + |
| 48 | +Thread -> Worker : run() |
| 49 | +activate Worker |
| 50 | + Worker -> ThreadPoolExecutor : runWorker(this); |
| 51 | + activate ThreadPoolExecutor |
| 52 | + ThreadPoolExecutor -> ThreadPoolExecutor : beforeExecute(wt, task); |
| 53 | + ThreadPoolExecutor -> command : run() |
| 54 | + activate command |
| 55 | + note over command |
| 56 | + 需要执行的任务,当前任务是 |
| 57 | + System.out.println("Hello world"); |
| 58 | + end note |
| 59 | + deactivate command |
| 60 | + ThreadPoolExecutor -> ThreadPoolExecutor : afterExecute(task, thrown); |
| 61 | + ThreadPoolExecutor -> ThreadPoolExecutor : getTask() |
| 62 | + activate ThreadPoolExecutor |
| 63 | + ThreadPoolExecutor -> ThreadPoolExecutor : compareAndDecrementWorkerCount() |
| 64 | + deactivate ThreadPoolExecutor |
| 65 | + ThreadPoolExecutor -> ThreadPoolExecutor : processWorkerExit() |
| 66 | + activate ThreadPoolExecutor |
| 67 | + ThreadPoolExecutor -> ThreadPoolExecutor : tryTerminate() |
| 68 | + activate ThreadPoolExecutor |
| 69 | + ThreadPoolExecutor -> ThreadPoolExecutor : ctl.compareAndSet(c, ctlOf(TIDYING, 0)) |
| 70 | + ThreadPoolExecutor -> ThreadPoolExecutor : terminated() |
| 71 | + ThreadPoolExecutor -> ThreadPoolExecutor : ctl.set(ctlOf(TERMINATED, 0)); |
| 72 | + deactivate ThreadPoolExecutor |
| 73 | + deactivate ThreadPoolExecutor |
| 74 | + deactivate ThreadPoolExecutor |
| 75 | +deactivate Worker |
| 76 | + |
| 77 | +@enduml |
0 commit comments