java多线程 线程池

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java多线程 线程池相关的知识,希望对你有一定的参考价值。

如何去设计一个自己的线程池?
思路与生产者与消费者模式相同,将任务放到队列中,子线程再从队列中取出任务去执行。

方式一:固定线程池,一开始是就申请好线程。
比如:
公司一次性雇佣5个工人,往后在接手任务还是这5 个人去做。
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class ThreadPoolV1{
    private BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(10);//队列容量为10,大于就阻塞等待。
    private Thread[] workers = new Thread[5];//线程的个数即一开始雇佣的工人个数
    ThreadPoolV1() {
        for (int i = 0; i < 5; i++) {
            workers[i] = new Worker(workQueue);
            workers[i].start();//5个线程的启动 去完成业务
        }
    }
    public void execute(Runnable cmd) throws InterruptedException {
        workQueue.put(cmd);//将任务放到队列中
    }

    private static class Worker extends Thread {
        private BlockingQueue<Runnable> workQueue;

        Worker(BlockingQueue<Runnable> queue) {
            workQueue = queue;
        }

        @Override
        public void run() {
            while (!isInterrupted()) {
                try {
                    Runnable cmd = workQueue.take();//从队列中把任务取出来
                    cmd.run();//业务

                } catch (InterruptedException e) {
                    break;
                }
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        ThreadPoolV1 pool = new ThreadPoolV1();
        pool.execute(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    System.out.println("第一个事情");
                }
            }
        });
        pool.execute(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    System.out.println("第二个时期");
                }
            }
        });
        pool.execute(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    System.out.println("第三 个时期");
                }
            }
        });
        pool.execute(new Runnable() {
            @Override
            public void run() {
                while (true) 
                    System.out.println("第四个时期");
                }

            }
        });
        pool.execute(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    System.out.println("第五个时期");
                }
            }
        });
                 pool.execute(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    System.out.println("第五个时期");
                }
            }
        });
                }
    ```

方式二:当有任务时雇佣一个工人,再来任务时再雇佣一个工人,一直这样,直到雇佣人数达到预期最大值,再来任务就放到队列中去。

public class ThreadPoolV2 {
    private BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(10);//队列上大于10个任务时,发生阻塞等待
    private int maxThreads = 5;
    private int currentThreads = 0;//一开始没有线程
    private Thread[] works = new Thread[maxThreads];
    public void execute(Runnable cmd) throws InterruptedException {
        if (currentThreads == maxThreads) {//雇佣人数达到最大值
            workQueue.put(cmd);//任务放到队列
        } else {
            Worker worker = new Worker(workQueue);//雇佣一个工人
           works[currentThreads++] = worker;
            worker.start();
            workQueue.put(cmd);
        }
    }
   private static class Worker extends Thread {
        private BlockingQueue<Runnable> workQueue;
        Worker(BlockingQueue<Runnable> queue) {
            workQueue = queue;
        }
        @Override
        public void run() {
            while (!isInterrupted()) {
                try {
                    Runnable cmd = workQueue.take();//从
                    cmd.run();
                } catch (InterruptedException e) {
                    break;
                }
            }
        }
    }
}
 public static void main(String[] args) throws InterruptedException {
        ThreadPoolV1 pool = new ThreadPoolV1();
        pool.execute(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    System.out.println("第一个事情");
                }
            }
        });
        pool.execute(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    System.out.println("第二个时期");
                }
            }
        });
        pool.execute(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    System.out.println("第三 个时期");
                }
            }
        });
        pool.execute(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    System.out.println("第四个时期");
                }
            }
        });
        pool.execute(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    System.out.println("第五个时期");
                }
            }
        });
        pool.execute(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    System.out.println("第六个时期");
                }
            }
        });
    }
}

以上是关于java多线程 线程池的主要内容,如果未能解决你的问题,请参考以下文章

Java——线程池

Android 多线程下载,断点续传,线程池

Java线程池详解

Java线程池详解

Java 线程池详解

newCacheThreadPool()newFixedThreadPool()newScheduledThreadPool()newSingleThreadExecutor()自定义线程池(代码片段