ThreadPoolExectutor使用测试1-线程数量不超过coreSize大小

简介: ThreadPoolExectutor使用测试1-线程数量不超过coreSize大小

测试特性

  1. 核心线程大小为n,有界队列为m,当向线程池提交n+m个任务时,线程池中的线程数量不会超过核心线程的大小n

环境

jdk

1.8.x

maven

<!-- junit test -->
<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.12</version>
  <scope>test</scope>
</dependency>

测试代码

/**
 * ThreadPoolExecutor测试类
 */
public class ThreadPoolExecutorTest {

    @Test
    public void testThreadCountNotGreaterThanCoreSize() throws InterruptedException {
        // 线程池中线程索引
        AtomicInteger pooledThreadIdx = new AtomicInteger(0);
        
        // 新建线程池
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                1,
                3,
                60L * 1000,
                TimeUnit.MILLISECONDS,
                // 任务队列为最大排队为10的任务队列
                new ArrayBlockingQueue<>(10),
                // 定制ThreadFactory,定义线程名称,以在多个线程池场景下区分业务线程
                r -> new Thread(r, "executor-tester" + pooledThreadIdx.getAndIncrement()),
                
                // 如果排队数量超过10,且线程最大已经达到maximumPoolSize时,再有任务提交时的拒绝策略
                // 一般是直接拒绝:表示服务仅能支撑这么多
                new ThreadPoolExecutor.AbortPolicy()
        );

        AtomicBoolean pass = new AtomicBoolean(false);

        // 向线程池提交11个任务
        submitTask(threadPoolExecutor, pass, 11);

        // 控制线程
        Thread controlThread = new Thread(() -> {
            int i = 0;
            while (i++ < 10) {
                // 先将自己睡眠一秒防止线程池还没有“反应过来”就获取活动线程数量为0的问题
                sleep(1000);
                
                // 睡眠一秒后再获取活动的线程数量应该为1,
                Assert.assertEquals(1, threadPoolExecutor.getActiveCount());
                StdOut.println("thread pool running workers: " + threadPoolExecutor.getActiveCount());
            }

            // 将线程中的任务全部放行
            pass.set(true);

            i = 0;
            // 等待大约2秒时间再判断线程池中的活动线程数量应该为0
            // 因为任务已经执行完成了
            while (i++ < 10) {
                sleep(200);
                StdOut.println("thread pool running workers: " + threadPoolExecutor.getActiveCount());
            }
            Assert.assertEquals(0, threadPoolExecutor.getActiveCount());
            StdOut.println("thread pool running workers: " + threadPoolExecutor.getActiveCount());
        });
        controlThread.start();
        controlThread.join();
    }

    private void submitTask(ThreadPoolExecutor threadPoolExecutor, AtomicBoolean pass, int taskCount) {
        for (int i = 0; i < taskCount; i++) {
            threadPoolExecutor.submit(() -> {
                while (!pass.get()) {
                    StdOut.println(Thread.currentThread().getName() + ": Thread running..." );
                    sleep(1000);
                }
            });
        }
    }

    public static void sleep(long millis) {
        try {
            Thread.sleep(millis);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

Console输出

executor-tester0: Thread running...
executor-tester0: Thread running...
thread pool running workers: 1
executor-tester0: Thread running...
thread pool running workers: 1
executor-tester0: Thread running...
thread pool running workers: 1
executor-tester0: Thread running...
thread pool running workers: 1
executor-tester0: Thread running...
thread pool running workers: 1
thread pool running workers: 1
executor-tester0: Thread running...
executor-tester0: Thread running...
thread pool running workers: 1
executor-tester0: Thread running...
thread pool running workers: 1
executor-tester0: Thread running...
thread pool running workers: 1
executor-tester0: Thread running...
thread pool running workers: 1
thread pool running workers: 1
thread pool running workers: 1
thread pool running workers: 1
thread pool running workers: 1
thread pool running workers: 0
thread pool running workers: 0
thread pool running workers: 0
thread pool running workers: 0
thread pool running workers: 0
thread pool running workers: 0
thread pool running workers: 0
Disconnected from the target VM, address: '127.0.0.1:52409', transport: 'socket'

Process finished with exit code 0
目录
相关文章
|
2天前
|
安全 前端开发 测试技术
《C++ Concurrencyin Action》第10章--多线程程序的测试和调试
《C++ Concurrencyin Action》第10章--多线程程序的测试和调试
|
2天前
|
存储 安全 Java
手撕线程池与性能测试
手撕线程池与性能测试
67 0
|
2天前
|
安全 Java
ConcurrentHashMap扩容的详细介绍以及多线程测试(基于JDK1.8)
ConcurrentHashMap扩容的详细介绍以及多线程测试(基于JDK1.8) ConcurrentHashMap是Java中线程安全的哈希表实现。它使用了锁分段技术,将哈希表分成多个Segment(默认为16),每个Segment都是一个独立的哈希表,每个Segment内部维护一个ReentrantLock锁,对于不同的Segment,它们可以被并发访问。当进行put、get等操作时,只需要获取对应Segment的锁即可,大大提高了并发访问的效率。
|
存储 关系型数据库 MySQL
linux Command sysbench 线程压力测试工具(2)
linux Command sysbench 线程压力测试工具(2)
|
Oracle Ubuntu 关系型数据库
linux Command sysbench 线程压力测试工具(1)
linux Command sysbench 线程压力测试工具(1)
|
算法 Java 测试技术
【DVCon-US-2020】基于多线程UVM测试平台的仿真加速方法
【DVCon-US-2020】基于多线程UVM测试平台的仿真加速方法
278 0
【DVCon-US-2020】基于多线程UVM测试平台的仿真加速方法
|
Web App开发 移动开发 前端开发
Selenium+Python3之:多线程进行跨浏览器测试
Selenium+Python3之:多线程进行跨浏览器测试
226 1
Selenium+Python3之:多线程进行跨浏览器测试
|
索引 Python
python 线程 ~~ ~~~为面试开辟VIP通道~~~~~测试、死锁、全局变量共享、守护主线程等。。。。。。(2)
python 线程 ~~ ~~~为面试开辟VIP通道~~~~~测试、死锁、全局变量共享、守护主线程等。。。。。。(2)
122 0
python 线程 ~~ ~~~为面试开辟VIP通道~~~~~测试、死锁、全局变量共享、守护主线程等。。。。。。(2)
|
存储 JSON 资源调度
python 线程 ~~ ~~~为面试开辟VIP通道~~~~~测试、死锁、全局变量共享、守护主线程等。。。。。。(1)
线程(英语:thread)是操作系统能够进行运算调度的最小单位。线程很重要,通过本篇文章可以让你们很好的了解线程的传参、线程执行规则、守护主线程、线程间共享全局变量、进程互斥锁、死锁进程怎么解决。希望对你们有所帮助。
206 0
python 线程 ~~ ~~~为面试开辟VIP通道~~~~~测试、死锁、全局变量共享、守护主线程等。。。。。。(1)
java94-cpu随机调用线程测试
java94-cpu随机调用线程测试
192 0
java94-cpu随机调用线程测试