• 【Java并发编程十二】线程池


    线程池

     用来统一地管理线程,避免线程的重复创建与销毁。使用线程池可以让执行完的线程回到线程池,等待下一次调用。

    import jdk.jshell.EvalException;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantLock;
    import java.util.concurrent.locks.ReentrantReadWriteLock;
    public class myTest implements Runnable{
        public static void main(String[] args) {
            myTest test = new myTest();
            ExecutorService service = Executors.newFixedThreadPool(5);
            for (int i = 0; i < 10; i++) {
                service.submit(test);
            }
            service.shutdown();
        }
        @Override
        public void run() {
            System.out.println(System.currentTimeMillis()+" ID: "+Thread.currentThread().getId());
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    线程池的种类

    package myTest;
    
    import jdk.jshell.EvalException;
    
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantLock;
    import java.util.concurrent.locks.ReentrantReadWriteLock;
    public class myTest implements Runnable{
        public static void main(String[] args) {
            myTest test = new myTest();
            // 创建一个拥有固定数量的线程池
            ExecutorService service1 = Executors.newFixedThreadPool(3);
            // 创建一个只有一个线程的线程池
            ExecutorService service2 = Executors.newSingleThreadExecutor();
            // 创建一个拥有自动改变大小的线程池
            ExecutorService service3 = Executors.newCachedThreadPool();
            // 创建一个有固定时间执行的单个线程池
            ExecutorService service4 = Executors.newSingleThreadScheduledExecutor();
            // 创建一个有固定时间执行的且指定数量的线程池
            ExecutorService service5 = Executors.newScheduledThreadPool(3);
        }
        @Override
        public void run() {
            System.out.println(System.currentTimeMillis()+" ID: "+Thread.currentThread().getId());
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    ThreadPoolExecutor 线程池的内部实现

     我们可以发现线程池的构造方法来源于一个ThreadPoolExecutor类。

        public static ExecutorService newFixedThreadPool(int nThreads) {
            return new ThreadPoolExecutor(nThreads, nThreads,
                                          0L, TimeUnit.MILLISECONDS,
                                          new LinkedBlockingQueue<Runnable>());
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    public ThreadPoolExecutor(
    	// 核心线程池大小
        int corePoolSize,
        // 最大线程池大小
        int maximumPoolSize,
        // 线程池中超过corePoolSize数目的空闲线程最大存活时间
        long keepAliveTime,
        // keepAliveTime时间单位
        TimeUnit unit,
        // 阻塞任务队列
        BlockingQueue<Runnable> workQueue) {
            this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
                 Executors.defaultThreadFactory(), defaultHandler);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
  • 相关阅读:
    Java_代码块
    【数据结构——顺序表】线性表很难嘛?这篇文章能让你轻松掌握顺序表
    元数据管理系统
    突出最强算法模型——回归算法 !!
    bukku ctf(刷题2)
    从零学习 InfiniBand-network架构(七) ——IB协议中数据如何传输
    《Linux运维总结:ARM64架构CPU基于docker-compose一离线部署rabbitmq 3.10.25容器版镜像模式集群》
    「小邓观点」SIEM解决方案的九大组件
    RabbitMQ篇
    Android开发基础——Activity和Intent
  • 原文地址:https://blog.csdn.net/qq_45722630/article/details/134563620