zoukankan      html  css  js  c++  java
  • 自己实现一个简单的线程池

    package how2j.threadTest;
    
    import java.util.LinkedList;
    
    public class ThreadPool {
        private int threadPoolSize = 10;  //线程池的默认大小
        private LinkedList<Runnable> linkedList = new LinkedList<>();   //用于存放提交到线程池的任务
    
        public ThreadPool(int threadPoolSize) {
            if (threadPoolSize > 0)
                this.threadPoolSize = threadPoolSize;
            //启动线程
            synchronized (linkedList) {
                for (int i = 0; i < this.threadPoolSize; i++) {
                    new MyThread("线程-->" + i).start();
                }
            }
        }
    
        public void add(Runnable runnable) {
            synchronized (linkedList) {
                linkedList.add(runnable);
                linkedList.notifyAll();
            }
        }
    
        class MyThread extends Thread {
            Runnable task = null;
    
            public MyThread(String name) {
                super(name);
            }
    
            @Override
            public void run() {
                System.out.println(this.getName() + ",启动了");
                while (true) {
                    //不断从任务队列中检查是否有任务需要进行
                    synchronized (linkedList) {
                        while (linkedList.isEmpty()) {
                            try {
                                linkedList.wait();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                        task = linkedList.removeFirst();
                    }
                    task.run();
                    System.out.println(this.getName() + ",得到执行");
                }
    
    
            }
        }
    
        public static void main(String[] args) {
            ThreadPool threadPool = new ThreadPool(20);
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            };
    
            for (int i = 0; i < 20; i++) {
                threadPool.add(runnable);
            }
        }
    }

     执行结果:

    线程-->11,启动了
    线程-->8,启动了
    线程-->3,启动了
    线程-->7,启动了
    线程-->6,启动了
    线程-->19,启动了
    线程-->14,启动了
    线程-->1,启动了
    线程-->2,启动了
    线程-->5,启动了
    线程-->15,启动了
    线程-->4,启动了
    线程-->13,启动了
    线程-->17,启动了
    线程-->18,启动了
    线程-->9,启动了
    线程-->10,启动了
    线程-->12,启动了
    线程-->0,启动了
    线程-->16,启动了
    线程-->15,得到执行
    线程-->5,得到执行
    线程-->8,得到执行
    线程-->3,得到执行
    线程-->7,得到执行
    线程-->19,得到执行
    线程-->12,得到执行
    线程-->14,得到执行
    线程-->1,得到执行
    线程-->11,得到执行
    线程-->10,得到执行
    线程-->9,得到执行
    线程-->2,得到执行
    线程-->16,得到执行
    线程-->17,得到执行
    线程-->0,得到执行
    线程-->18,得到执行
    线程-->4,得到执行
    线程-->13,得到执行
    线程-->6,得到执行
  • 相关阅读:
    2006: [NOI2010]超级钢琴
    3640: JC的小苹果
    2005: [Noi2010]能量采集
    金蝶云星空修改密码策略的SQL脚本
    关于 springboot 过滤器中使用@Autowired 为空 以及 使用 @Value 无法读取yml配置的问题解决
    CAS .NET Client 循环重定向的解决办法
    K/3 Cloud SSO配置
    K/3 Cloud SSO配置答疑
    K/3 Cloud 元数据表
    AngularJS select中ngOptions用法详解【转】
  • 原文地址:https://www.cnblogs.com/ustc-anmin/p/11841637.html
Copyright © 2011-2022 走看看