• 自己实现一个简单的线程池


    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,得到执行
  • 相关阅读:
    js定位光标到输入框指定位置
    JS获取本机时间和实时动态时间代码
    一个小游戏
    select optionschange oeder
    js控制下拉列表框
    glow滤镜的使用
    body.innerHTML
    怎样用C语言编写病毒(三)
    2011东北地区赛G题(二分网络流判可行性)
    Codeforces Round #122 (Div. 1)>TLE代码 跪求(n^2)的最小割顶集算法(StoerWagner)
  • 原文地址:https://www.cnblogs.com/ustc-anmin/p/11841637.html
Copyright © 2020-2023  润新知