并发编程:并发测试:监测Executor框架
程序员文章站
2022-06-26 18:15:58
监测Executor框架...
目录
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.
本文地址:https://blog.csdn.net/Bof_jangle/article/details/109261523
上一篇: java二维码生成器
下一篇: 首创证券伏劲松:云计算将革掉CIO的命
推荐阅读
-
并发编程之Disruptor并发框架
-
Java并发编程:Callable、Future和FutureTask---转载测试
-
Python并发编程之学习异步IO框架:asyncio 中篇(十)
-
Java并发编程之Executor接口的使用
-
Java 并发编程之ForkJoin框架
-
Python并发编程之实战异步IO框架:asyncio 下篇(十一)
-
并发编程:fork/join框架:创建一个fork/join框架(RecursiveAction)
-
Java并发编程之Fork/Join框架的理解
-
并发编程:并发测试:监测Executor框架
-
BAT必备500道面试题:设计模式+开源框架+并发编程+微服务等免费领取!