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,得到执行
  • 相关阅读:
    LeetCode Path Sum II
    LeetCode Longest Palindromic Substring
    LeetCode Populating Next Right Pointers in Each Node II
    LeetCode Best Time to Buy and Sell Stock III
    LeetCode Binary Tree Maximum Path Sum
    LeetCode Find Peak Element
    LeetCode Maximum Product Subarray
    LeetCode Intersection of Two Linked Lists
    一天一个设计模式(1)——工厂模式
    PHP迭代器 Iterator
  • 原文地址:https://www.cnblogs.com/ustc-anmin/p/11841637.html
Copyright © 2011-2022 走看看