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
目录
相关文章
|
18天前
|
存储 测试技术
【工作实践(多线程)】十个线程任务生成720w测试数据对系统进行性能测试
【工作实践(多线程)】十个线程任务生成720w测试数据对系统进行性能测试
29 0
【工作实践(多线程)】十个线程任务生成720w测试数据对系统进行性能测试
|
25天前
|
安全 Java
如何测试map对象的线程不安全
【6月更文挑战第20天】如何测试map对象的线程不安全
16 0
|
2月前
|
安全 前端开发 测试技术
《C++ Concurrencyin Action》第10章--多线程程序的测试和调试
《C++ Concurrencyin Action》第10章--多线程程序的测试和调试
|
2月前
|
存储 安全 Java
手撕线程池与性能测试
手撕线程池与性能测试
76 0
|
2月前
|
安全 Java
ConcurrentHashMap扩容的详细介绍以及多线程测试(基于JDK1.8)
ConcurrentHashMap扩容的详细介绍以及多线程测试(基于JDK1.8) ConcurrentHashMap是Java中线程安全的哈希表实现。它使用了锁分段技术,将哈希表分成多个Segment(默认为16),每个Segment都是一个独立的哈希表,每个Segment内部维护一个ReentrantLock锁,对于不同的Segment,它们可以被并发访问。当进行put、get等操作时,只需要获取对应Segment的锁即可,大大提高了并发访问的效率。
|
测试技术 调度
性能测试(9)——线程组详解
线程数:虚拟用户数 Ramp-Up时间(秒):启动全部虚拟用户数所需要的时间 循环次数:指定次数或勾选永远。使用调度器时,必须勾选永远 延迟创建线程直到需要:测试开始的时候,所有线程就被创建完。勾选了此选项,那么线程只会在合适的需要用到的时候创建 调度器:勾选后,调度器配置才能使用;
140 0
|
测试技术 调度
性能测试|JMeter线程组设置
性能测试|JMeter线程组设置
165 0
性能测试|JMeter线程组设置
|
Web App开发 移动开发 前端开发
Selenium+Python3之:多线程进行跨浏览器测试
Selenium+Python3之:多线程进行跨浏览器测试
249 1
Selenium+Python3之:多线程进行跨浏览器测试
|
算法 Java 测试技术
【DVCon-US-2020】基于多线程UVM测试平台的仿真加速方法
【DVCon-US-2020】基于多线程UVM测试平台的仿真加速方法
318 0
【DVCon-US-2020】基于多线程UVM测试平台的仿真加速方法
|
存储 关系型数据库 MySQL
linux Command sysbench 线程压力测试工具(2)
linux Command sysbench 线程压力测试工具(2)