多线程并发

动手实现一个简单的线程池来理解其原理

2021-05-15  本文已影响0人  肥兔子爱豆畜子

线程池的原理

简单来说,就是对外提供一个方法用来提交task,然后内部有一个task队列存放提交上来的任务,内部启动了若干个worker线程,这些worker线程不断地从task队列里拿任务执行。这就是线程池的简单原理。

线程池的作用

实现代码

练习实现一个简单线程池。这个简单线程池里边有1个阻塞队列和两个固定的core worker线程组成。

public class MyThreadPool {
    private static Logger logger = LoggerFactory.getLogger(MyThreadPool.class);
    private ArrayBlockingQueue<Runnable> taskQueue = new ArrayBlockingQueue<Runnable>(150); //任务队列
    
    public MyThreadPool() {
        super();
        for(int i=0; i<2; i++) {    //启动两个core worker线程
            Thread worker = new Thread(new Runnable() {

                @Override
                public void run() {
                    while(true) { //worker线程负责不断的从任务队列里阻塞的拿任务执行
                        try {
                            Runnable r = taskQueue.take(); //阻塞的从任务队列获取一个任务
                            r.run();  //执行具体的任务内容
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
                
            });
            worker.setName("MyThreadPool-" + i);
            worker.start();
        }
    }
    public void execute(Runnable task) {
        taskQueue.add(task);
    }
}

测试一下这个简单线程池:

MyThreadPool threadPool = new MyThreadPool();
        Runnable task1 = new Runnable() {
            @Override
            public void run() {
                int i = 0;
                while(i<10) {
                    i++;
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    logger.info("Hello world A.");
                }
            }};
            Runnable task2 = new Runnable() {
                @Override
                public void run() {
                    int i = 0;
                    while(i<10) {
                        i++;
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        logger.info("Hello world B.");
                    }
                }};
                Runnable task3 = new Runnable() {
                    @Override
                    public void run() {
                        int i = 0;
                        while(i<10) {
                            i++;
                            try {
                                Thread.sleep(1000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            logger.info("Hello world C.");
                        }
                    }};
        threadPool.execute(task1);
        logger.info("已向MyThreadPool提交task1");
        threadPool.execute(task2);
        logger.info("已向MyThreadPool提交task2");
        threadPool.execute(task3);
        logger.info("已向MyThreadPool提交task3");

运行效果:

[main] com.wangan.tp.MyThreadPool [codeline:88] - 已向MyThreadPool提交task1
[main] com.wangan.tp.MyThreadPool [codeline:90] - 已向MyThreadPool提交task2
[main] com.wangan.tp.MyThreadPool [codeline:92] - 已向MyThreadPool提交task3
[MyThreadPool-1] com.wangan.tp.MyThreadPool [codeline:70] - Hello world B.
[MyThreadPool-0] com.wangan.tp.MyThreadPool [codeline:56] - Hello world A.
[MyThreadPool-0] com.wangan.tp.MyThreadPool [codeline:56] - Hello world A.
[MyThreadPool-1] com.wangan.tp.MyThreadPool [codeline:70] - Hello world B.
[MyThreadPool-0] com.wangan.tp.MyThreadPool [codeline:56] - Hello world A.
[MyThreadPool-1] com.wangan.tp.MyThreadPool [codeline:70] - Hello world B.
[MyThreadPool-1] com.wangan.tp.MyThreadPool [codeline:70] - Hello world B.
[MyThreadPool-0] com.wangan.tp.MyThreadPool [codeline:56] - Hello world A.
[MyThreadPool-0] com.wangan.tp.MyThreadPool [codeline:56] - Hello world A.
[MyThreadPool-1] com.wangan.tp.MyThreadPool [codeline:70] - Hello world B.
[MyThreadPool-0] com.wangan.tp.MyThreadPool [codeline:56] - Hello world A.
[MyThreadPool-1] com.wangan.tp.MyThreadPool [codeline:70] - Hello world B.
[MyThreadPool-1] com.wangan.tp.MyThreadPool [codeline:70] - Hello world B.
[MyThreadPool-0] com.wangan.tp.MyThreadPool [codeline:56] - Hello world A.
[MyThreadPool-0] com.wangan.tp.MyThreadPool [codeline:56] - Hello world A.
[MyThreadPool-1] com.wangan.tp.MyThreadPool [codeline:70] - Hello world B.
[MyThreadPool-0] com.wangan.tp.MyThreadPool [codeline:56] - Hello world A.
[MyThreadPool-1] com.wangan.tp.MyThreadPool [codeline:70] - Hello world B.
[MyThreadPool-1] com.wangan.tp.MyThreadPool [codeline:70] - Hello world B.
[MyThreadPool-0] com.wangan.tp.MyThreadPool [codeline:56] - Hello world A.
[MyThreadPool-1] com.wangan.tp.MyThreadPool [codeline:84] - Hello world C.
[MyThreadPool-1] com.wangan.tp.MyThreadPool [codeline:84] - Hello world C.
[MyThreadPool-1] com.wangan.tp.MyThreadPool [codeline:84] - Hello world C.
[MyThreadPool-1] com.wangan.tp.MyThreadPool [codeline:84] - Hello world C.
[MyThreadPool-1] com.wangan.tp.MyThreadPool [codeline:84] - Hello world C.
[MyThreadPool-1] com.wangan.tp.MyThreadPool [codeline:84] - Hello world C.
[MyThreadPool-1] com.wangan.tp.MyThreadPool [codeline:84] - Hello world C.
[MyThreadPool-1] com.wangan.tp.MyThreadPool [codeline:84] - Hello world C.
[MyThreadPool-1] com.wangan.tp.MyThreadPool [codeline:84] - Hello world C.
[MyThreadPool-1] com.wangan.tp.MyThreadPool [codeline:84] - Hello world C.
上一篇下一篇

猜你喜欢

热点阅读