美文网首页多线程
java多线程的实现(创建一个线程池并且简单的使用)

java多线程的实现(创建一个线程池并且简单的使用)

作者: 爱吃苹果的西瓜 | 来源:发表于2019-08-04 14:31 被阅读0次

    什么时候用多线程?

    image.png
    程序执行结果:
    image.png
    先说一下此处的打印,第一个参数是当前线程名称,由于线程之间是异步执行,有的还没创建好,有的后来居上就执行完了,打印线程的名称会这样,第二个参数是优先级,默认都是5,第三个参数是线程组名称。

    github地址:https://github.com/furtech/java_utils/blob/master/src/main/java/com/furtech/javautils/ThreadPool.java

    package com.furtech.javautils;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    import java.util.concurrent.LinkedBlockingQueue;
    
    /**
     * @des 线程池的简单实现(可扩展)
     *
     * @author 719383495@qq.com | 719383495qq@gmail.com | 有问题可以邮箱或者github联系我
     * @date 2019/8/4 13:55
     */
    public class ThreadPool {
        /**@des logger */
        private static final Logger logger = LoggerFactory.getLogger(ThreadPool.class);
        private final int poolSize;
        private final LinkedBlockingQueue queue;
        private final PoolWorker[] runable;
    
        public ThreadPool(int poolSize) {
            this.poolSize = poolSize;
            queue = new LinkedBlockingQueue();
            runable = new PoolWorker[poolSize];
            for (int i = 0; i < poolSize; i++) {
                runable[i] = new PoolWorker();
                new Thread(runable[i], "pool-" + i).start();
            }
        }
    
        public void execute(Runnable task) {
            synchronized (queue) {
                queue.add(task);
                queue.notify();
            }
        }
    
        private class PoolWorker implements Runnable {
            @Override
            public void run() {
                Runnable task ;
    
                while (true) {
                    synchronized (queue) {
                        while (queue.isEmpty()) {
                            try {
                                queue.wait();
                            } catch (Exception e) {
                                logger.info("exception in queue waiting :{}",e.getMessage());
                            }
                        }
                        task = (Runnable) queue.poll();
                    }
                    try {
                        task.run();
                    } catch (RuntimeException e) {
                        logger.info("run exception : {}", e.getMessage());
                    }
    
                }
            }
        }
    
    }
    
    class ThreadPoolMain {
        public static void main(String[] args) {
            ThreadPool pool = new ThreadPool(5);
            int MaxSize = 100;
            for (int i = 0; i < MaxSize; i++) {
                pool.execute(() -> System.out.println(Thread.currentThread()));
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    
    
    

    我的博客即将同步至腾讯云+社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=39wh3ifp3dkws

    相关文章

      网友评论

        本文标题:java多线程的实现(创建一个线程池并且简单的使用)

        本文链接:https://www.haomeiwen.com/subject/ovfydctx.html