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

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

@Slf4j
public class TheRunnable implements Runnable {

    private String username;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public TheRunnable(String username){
        super();
        this.username=username;
    }



    @Override
    public void run() {
        try {
            Thread.sleep(2000);
            log.info("线程名:"+username);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

运行类代码如下:

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

运行结果如下:

10:37:06.675 [pool-1-thread-5] INFO com.ozx.concurrentprogram.executor.service.TheRunnable - 线程名:G5
10:37:06.660 [pool-1-thread-3] INFO com.ozx.concurrentprogram.executor.service.TheRunnable - 线程名:G3
10:37:06.675 [pool-1-thread-1] INFO com.ozx.concurrentprogram.executor.service.TheRunnable - 线程名:G1
10:37:06.660 [pool-1-thread-2] INFO com.ozx.concurrentprogram.executor.service.TheRunnable - 线程名:G2
10:37:06.706 [pool-1-thread-4] INFO com.ozx.concurrentprogram.executor.service.TheRunnable - 线程名:G4
10:37:08.692 [pool-1-thread-3] INFO com.ozx.concurrentprogram.executor.service.TheRunnable - 线程名:G6
10:37:08.692 [pool-1-thread-1] INFO com.ozx.concurrentprogram.executor.service.TheRunnable - 线程名:G7
10:37:08.692 [pool-1-thread-5] INFO com.ozx.concurrentprogram.executor.service.TheRunnable - 线程名:G8
10:37:08.692 [pool-1-thread-2] INFO com.ozx.concurrentprogram.executor.service.TheRunnable - 线程名:G9
10:37:08.723 [pool-1-thread-4] INFO com.ozx.concurrentprogram.executor.service.TheRunnable - 线程名:G10
10:37:10.708 [pool-1-thread-3] INFO com.ozx.concurrentprogram.executor.service.TheRunnable - 线程名:G11
10:37:10.708 [pool-1-thread-1] INFO com.ozx.concurrentprogram.executor.service.TheRunnable - 线程名:G12
10:37:10.708 [pool-1-thread-5] INFO com.ozx.concurrentprogram.executor.service.TheRunnable - 线程名:G13
10:37:10.708 [pool-1-thread-2] INFO com.ozx.concurrentprogram.executor.service.TheRunnable - 线程名:G14
10:37:10.739 [pool-1-thread-4] INFO com.ozx.concurrentprogram.executor.service.TheRunnable - 线程名:G15
10:37:12.714 [pool-1-thread-1] INFO com.ozx.concurrentprogram.executor.service.TheRunnable - 线程名:G17
10:37:12.714 [pool-1-thread-3] INFO com.ozx.concurrentprogram.executor.service.TheRunnable - 线程名:G16
10:37:12.714 [pool-1-thread-5] INFO com.ozx.concurrentprogram.executor.service.TheRunnable - 线程名:G18
10:37:12.714 [pool-1-thread-2] INFO com.ozx.concurrentprogram.executor.service.TheRunnable - 线程名:G19
10:37:12.745 [pool-1-thread-4] INFO com.ozx.concurrentprogram.executor.service.TheRunnable - 线程名:G20

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值