目录
Executor
- getCorePoolSize(): 维持内部线程池所需的最少线程数,尽管没有任何任务在线程池中执行。
- getPoolSize():内部线程池实际大小。
- getActiveCount():正在执行任务的线程数。
- getTaskCount():已安排执行计划的任务数。
- getCompletedTaskCount():已经执行并完成执行计划的任务数
一、主程序
package xyz.jangle.thread.test.n9_4.testexecutor;
import java.util.Random;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* 9.4、监测Executor类
* @author jangle
* @email jangle@jangle.xyz
* @time 2020年10月24日 下午4:01:48
*
*/
public class M {
public static void main(String[] args) throws InterruptedException {
ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newCachedThreadPool();
var random = new Random();
for (int i = 0; i < 10; i++) {
var task = new Task(random.nextInt(10000));
executor.submit(task);
}
for (int i = 0; i < 5; i++) {
showLog(executor);
TimeUnit.SECONDS.sleep(1);
}
executor.shutdown();
for (int i = 0; i < 5; i++) {
showLog(executor);
TimeUnit.SECONDS.sleep(1);
}
executor.awaitTermination(1, TimeUnit.DAYS);
System.out.println("M:End of the program.");
}
private static void showLog(ThreadPoolExecutor executor) {
System.out.println("**************************");
System.out.println("executor.getCorePoolSize(): " + executor.getCorePoolSize());
System.out.println("executor.getPoolSize(): " + executor.getPoolSize());
System.out.println("executor.getActiveCount(): " + executor.getActiveCount());
System.out.println("executor.getTaskCount(): " + executor.getTaskCount());
System.out.println("executor.getCompletedTaskCount(): " + executor.getCompletedTaskCount());
System.out.println("executor.isShutdown(): " + executor.isShutdown());
System.out.println("executor.isTerminating(): " + executor.isTerminating());
System.out.println("executor.isTerminated(): " + executor.isTerminated());
System.out.println("**************************");
}
}
二、普通任务
package xyz.jangle.thread.test.n9_4.testexecutor;
import java.util.concurrent.TimeUnit;
/**
* @author jangle
* @email jangle@jangle.xyz
* @time 2020年10月24日 下午4:05:48
*
*/
public class Task implements Runnable {
private final long milliseconds;
public Task(long milliseconds) {
super();
this.milliseconds = milliseconds;
}
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + ":Begin ");
try {
TimeUnit.MILLISECONDS.sleep(milliseconds);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " :End ");
}
}
三、执行结果
pool-1-thread-4:Begin
pool-1-thread-10:Begin
pool-1-thread-1:Begin
pool-1-thread-5:Begin
pool-1-thread-6:Begin
pool-1-thread-3:Begin
**************************
pool-1-thread-2:Begin
pool-1-thread-9:Begin
pool-1-thread-8:Begin
pool-1-thread-7:Begin
executor.getCorePoolSize(): 0
executor.getPoolSize(): 10
executor.getActiveCount(): 10
executor.getTaskCount(): 10
executor.getCompletedTaskCount(): 0
executor.isShutdown(): false
executor.isTerminating(): false
executor.isTerminated(): false
**************************
pool-1-thread-1 :End
**************************
executor.getCorePoolSize(): 0
executor.getPoolSize(): 10
executor.getActiveCount(): 9
executor.getTaskCount(): 10
executor.getCompletedTaskCount(): 1
executor.isShutdown(): false
executor.isTerminating(): false
executor.isTerminated(): false
**************************
pool-1-thread-6 :End
**************************
executor.getCorePoolSize(): 0
executor.getPoolSize(): 10
executor.getActiveCount(): 8
executor.getTaskCount(): 10
executor.getCompletedTaskCount(): 2
executor.isShutdown(): false
executor.isTerminating(): false
executor.isTerminated(): false
**************************
pool-1-thread-2 :End
**************************
executor.getCorePoolSize(): 0
executor.getPoolSize(): 10
executor.getActiveCount(): 7
executor.getTaskCount(): 10
executor.getCompletedTaskCount(): 3
executor.isShutdown(): false
executor.isTerminating(): false
executor.isTerminated(): false
**************************
**************************
executor.getCorePoolSize(): 0
executor.getPoolSize(): 10
executor.getActiveCount(): 7
executor.getTaskCount(): 10
executor.getCompletedTaskCount(): 3
executor.isShutdown(): false
executor.isTerminating(): false
executor.isTerminated(): false
**************************
pool-1-thread-10 :End
pool-1-thread-4 :End
**************************
executor.getCorePoolSize(): 0
executor.getPoolSize(): 10
executor.getActiveCount(): 5
executor.getTaskCount(): 10
executor.getCompletedTaskCount(): 5
executor.isShutdown(): true
executor.isTerminating(): true
executor.isTerminated(): false
**************************
**************************
executor.getCorePoolSize(): 0
executor.getPoolSize(): 5
executor.getActiveCount(): 5
executor.getTaskCount(): 10
executor.getCompletedTaskCount(): 5
executor.isShutdown(): true
executor.isTerminating(): true
executor.isTerminated(): false
**************************
pool-1-thread-7 :End
pool-1-thread-8 :End
**************************
executor.getCorePoolSize(): 0
executor.getPoolSize(): 3
executor.getActiveCount(): 3
executor.getTaskCount(): 10
executor.getCompletedTaskCount(): 7
executor.isShutdown(): true
executor.isTerminating(): true
executor.isTerminated(): false
**************************
pool-1-thread-3 :End
pool-1-thread-9 :End
**************************
executor.getCorePoolSize(): 0
executor.getPoolSize(): 1
executor.getActiveCount(): 1
executor.getTaskCount(): 10
executor.getCompletedTaskCount(): 9
executor.isShutdown(): true
executor.isTerminating(): true
executor.isTerminated(): false
**************************
pool-1-thread-5 :End
**************************
executor.getCorePoolSize(): 0
executor.getPoolSize(): 0
executor.getActiveCount(): 0
executor.getTaskCount(): 10
executor.getCompletedTaskCount(): 10
executor.isShutdown(): true
executor.isTerminating(): false
executor.isTerminated(): true
**************************
M:End of the program.