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

携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第5天,点击查看活动详情

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

猜你喜欢

转载自juejin.im/post/7127159255605444616