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


    线程池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开销,加快程序运行效率,进而对创建线程类的代码进行了封装。

  • 相关阅读:
    图计算(林子雨慕课课程)
    “蔚来杯“2022牛客暑期多校训练营10,签到题HFIE
    不断完成新项目才能升职?前首席工程师“怒喷”谷歌晋升机制
    Pico Neo4、Neo3开发手柄的使用交互监听
    Numpy使用
    Allegro如何使用快捷键快速切换层面操作指导
    PRS中增加PCA 1-10如何操作
    进来了解实现官网搜索引擎的三种方法
    opentelemetry+python+jaeger链路追踪相关使用备注
    什么是腾讯云关系型数据库(MySQL/SQL Server/MariaDB/PostgreSQL详解)
  • 原文地址:https://blog.csdn.net/LBWNB_Java/article/details/126153795