• 【并发编程】- 线程池执行任务乱序特性


    线程池ThreadPoolExecutor执行任务Runnable乱序特性
    接口Runnable在线程池ThreadPoolExecutor的队列中是按顺序取出,执行却是乱序的。
    线程执行代码如下:

    1. @Slf4j
    2. public class TheRunnable implements Runnable {
    3. private String username;
    4. public String getUsername() {
    5. return username;
    6. }
    7. public void setUsername(String username) {
    8. this.username = username;
    9. }
    10. public TheRunnable(String username){
    11. super();
    12. this.username=username;
    13. }
    14. @Override
    15. public void run() {
    16. try {
    17. Thread.sleep(2000);
    18. log.info("线程名:"+username);
    19. } catch (InterruptedException e) {
    20. e.printStackTrace();
    21. }
    22. }
    23. }

    运行类代码如下:

    1. public class DisorderedOrderRun {
    2. public static void main(String[] args) {
    3. ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(5, 10, 10, TimeUnit.SECONDS, new LinkedBlockingQueue<>());
    4. for (int i = 0; i < 20; i++) {
    5. TheRunnable theRunnable = new TheRunnable("G" + (i + 1));
    6. threadPoolExecutor.execute(theRunnable);
    7. }
    8. }
    9. }

    运行结果如下:

    1. 10:37:06.675 [pool-1-thread-5] INFO com.ozx.concurrentprogram.executor.service.TheRunnable - 线程名:G5
    2. 10:37:06.660 [pool-1-thread-3] INFO com.ozx.concurrentprogram.executor.service.TheRunnable - 线程名:G3
    3. 10:37:06.675 [pool-1-thread-1] INFO com.ozx.concurrentprogram.executor.service.TheRunnable - 线程名:G1
    4. 10:37:06.660 [pool-1-thread-2] INFO com.ozx.concurrentprogram.executor.service.TheRunnable - 线程名:G2
    5. 10:37:06.706 [pool-1-thread-4] INFO com.ozx.concurrentprogram.executor.service.TheRunnable - 线程名:G4
    6. 10:37:08.692 [pool-1-thread-3] INFO com.ozx.concurrentprogram.executor.service.TheRunnable - 线程名:G6
    7. 10:37:08.692 [pool-1-thread-1] INFO com.ozx.concurrentprogram.executor.service.TheRunnable - 线程名:G7
    8. 10:37:08.692 [pool-1-thread-5] INFO com.ozx.concurrentprogram.executor.service.TheRunnable - 线程名:G8
    9. 10:37:08.692 [pool-1-thread-2] INFO com.ozx.concurrentprogram.executor.service.TheRunnable - 线程名:G9
    10. 10:37:08.723 [pool-1-thread-4] INFO com.ozx.concurrentprogram.executor.service.TheRunnable - 线程名:G10
    11. 10:37:10.708 [pool-1-thread-3] INFO com.ozx.concurrentprogram.executor.service.TheRunnable - 线程名:G11
    12. 10:37:10.708 [pool-1-thread-1] INFO com.ozx.concurrentprogram.executor.service.TheRunnable - 线程名:G12
    13. 10:37:10.708 [pool-1-thread-5] INFO com.ozx.concurrentprogram.executor.service.TheRunnable - 线程名:G13
    14. 10:37:10.708 [pool-1-thread-2] INFO com.ozx.concurrentprogram.executor.service.TheRunnable - 线程名:G14
    15. 10:37:10.739 [pool-1-thread-4] INFO com.ozx.concurrentprogram.executor.service.TheRunnable - 线程名:G15
    16. 10:37:12.714 [pool-1-thread-1] INFO com.ozx.concurrentprogram.executor.service.TheRunnable - 线程名:G17
    17. 10:37:12.714 [pool-1-thread-3] INFO com.ozx.concurrentprogram.executor.service.TheRunnable - 线程名:G16
    18. 10:37:12.714 [pool-1-thread-5] INFO com.ozx.concurrentprogram.executor.service.TheRunnable - 线程名:G18
    19. 10:37:12.714 [pool-1-thread-2] INFO com.ozx.concurrentprogram.executor.service.TheRunnable - 线程名:G19
    20. 10:37:12.745 [pool-1-thread-4] INFO com.ozx.concurrentprogram.executor.service.TheRunnable - 线程名:G20

    从运行结果看出Runnable状态从就绪态到运行态转换,线程各自获取CPU时间片不一样,所以线程执行顺序是乱序的。
    通过使用线程池能最大幅度地减少创建线程对象的内存与CPU开销,加快程序运行效率,进而对创建线程类的代码进行了封装。

  • 相关阅读:
    qt day6 人脸识别
    基于minist数据集用VAE训练生成图片(VAE基础入门学习)
    postman使用pre-request script
    油气大数据平台建设案例分享,让油田数据同步效率提升20%的解决方案
    Android开发中System.currentTimeMillis()与SystemClock.uptimeMillis()
    FDTD script command(源/监视器)
    [GitLab CI/CD] 实践操作片段记录
    JVM之Class文件
    微服务架构中各个组件都需要使用哪些技术?
    lme4:用于混合效应模型分析的R包
  • 原文地址:https://blog.csdn.net/LBWNB_Java/article/details/126153795