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


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

  • 相关阅读:
    EasyRecovery15万能数据恢复软件全面详细功能讲解
    【Docker学习】docker login/logout
    N-FX2-4 移动链表节点
    Vue简介,模板语法,mvvm模型
    NetSuite 10个用户参数
    Qt 6 资料汇总
    Spring面试题之循环依赖与三级缓存
    Redis实现分布式锁
    【OSTEP】分页: 快速地址转换(TLB) | TLB命中处理 | ASID 与页共享 | TLB替换策略: LRU策略与随机策略 | Culler定律
    人生规划(Flag)
  • 原文地址:https://blog.csdn.net/LBWNB_Java/article/details/126153795