• Java线程池


      java.util.concurrent.Executors是Java提供的一个线程池工具类,通过该工具类,我们可以创建出不同类型的线程池。

      从这个工具类的api来看,Java提供了五种线程池,分别是:

    • FixedThreadPool
    • WorkStealingPool
    • SingleThreadExecutor
    • CachedThreadPool
    • ScheduledThreadPool

    FixedThreadPool

    package java.util.concurrent;
    
    public class Executors {
    
        public static ExecutorService newFixedThreadPool(int nThreads) {
            return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
        }public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) {
            return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), threadFactory);
        }
    
    }

    线程池的属性:

    • 核心线程数:创建线程池实例时指定(必需)
    • 最大线程数:与核心线程数一致
    • 空闲线程的存活时间:默认是0,没有限制
    • 存活时间单位:默认是纳秒
    • 线程工厂:可在创建线程池实例时指定(可选),默认默认Executors.defaultThreadFactory()
    • 任务队列:LinkedBlockingQueue

      
    WorkStealingPool

    package java.util.concurrent;
    
    public class Executors {public static ExecutorService newWorkStealingPool(int parallelism) {
            return new ForkJoinPool(parallelism, ForkJoinPool.defaultForkJoinWorkerThreadFactory, null, true);
        }
    
        public static ExecutorService newWorkStealingPool() {
            return new ForkJoinPool(Runtime.getRuntime().availableProcessors(), ForkJoinPool.defaultForkJoinWorkerThreadFactory, null, true);
        }
    
    }


    SingleThreadExecutor

    package java.util.concurrent;
    
    public class Executors {public static ExecutorService newSingleThreadExecutor() {
            return new FinalizableDelegatedExecutorService(new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()));
        }
    
        public static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory) {
            return new FinalizableDelegatedExecutorService(new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), threadFactory));
        }
    
    }

    线程池属性:

    • 核心线程数:1
    • 最大线程数:1
    • 空闲线程的存活时间:默认是0,没有限制
    • 存活时间单位:默认是纳秒
    • 任务队列:LinkedBlockingQueue
    • 线程工厂:默认Executors.defaultThreadFactory()


    CachedThreadPool

    package java.util.concurrent;
    
    public class Executors {public static ExecutorService newCachedThreadPool() {
            return new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>());
        }
    
        public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) {
            return new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(), threadFactory);
        }
    
    }

    线程池属性:

    • 核心线程数:0
    • 最大线程数:Integer.MAX_VALUE
    • 空闲线程的存活时间:默认是60
    • 存活时间单位:默认是秒
    • 任务队列:SynchronousQueue
    • 线程工厂:默认Executors.defaultThreadFactory()


    ScheduledThreadPool

    package java.util.concurrent;
    
    public class Executors {public static ScheduledExecutorService newSingleThreadScheduledExecutor() {
            return new DelegatedScheduledExecutorService(new ScheduledThreadPoolExecutor(1));
        }
    
        public static ScheduledExecutorService newSingleThreadScheduledExecutor(ThreadFactory threadFactory) {
            return new DelegatedScheduledExecutorService(new ScheduledThreadPoolExecutor(1, threadFactory));
        }
    
        public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
            return new ScheduledThreadPoolExecutor(corePoolSize);
        }
    
        public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize, ThreadFactory threadFactory) {
            return new ScheduledThreadPoolExecutor(corePoolSize, threadFactory);
        }
    
    }

    线程池属性:

    • 核心线程数:创建线程池实例时指定(必需)
    • 最大线程数:Integer.MAX_VALUE
    • 空闲线程的存活时间:默认是0,没有限制
    • 存活时间单位:默认是纳秒
    • 任务队列:DelayedWorkQueue
    • 线程工厂:默认Executors.defaultThreadFactory()
  • 相关阅读:
    最近迷上用dvd字幕学习英语
    原始套接字
    c语言socket编程
    inet_aton和inet_network和inet_addr三者比较
    用man来查找c函数库
    ubuntu的系统日志配置文件的位置
    复制文件
    vim复制粘贴解密(转)
    vim的自动补齐功能
    两个数据结构ip和tcphdr
  • 原文地址:https://www.cnblogs.com/517cn/p/10878836.html
Copyright © 2020-2023  润新知