`

读Executors源码

阅读更多
//一个管理线程创建的类里面都是静态方法

//创建一个corePoolSize和maximumPoolSize相等的线程池也就是当线程数为nThreads时,就不在新增线程了。
 public static ExecutorService newFixedThreadPool(int nThreads) {
        return new ThreadPoolExecutor(nThreads, nThreads,
                                      0L, TimeUnit.MILLISECONDS,
                                      new LinkedBlockingQueue<Runnable>());
    }

//和上面方法相比传入ThreadFactory相当于自己来管理线程的创建。
public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) {
        return new ThreadPoolExecutor(nThreads, nThreads,
                                      0L, TimeUnit.MILLISECONDS,
                                      new LinkedBlockingQueue<Runnable>(),
                                      threadFactory);
    }

//构建单个线程的线程池
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));
    }


//相当于缓存一个线程超过60秒没有任务执行则回收该线程
 public static ExecutorService newCachedThreadPool() {
        return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                      60L, TimeUnit.SECONDS,
                                      new SynchronousQueue<Runnable>());
    }

//相当于缓存一个线程超过60秒没有任务执行则回收该线程.自己管理线程的创建
public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) {
        return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                      60L, TimeUnit.SECONDS,
                                      new SynchronousQueue<Runnable>(),
                                      threadFactory);
    }


//创建只有一个线程的定时任务
 public static ScheduledExecutorService newSingleThreadScheduledExecutor() {
        return new DelegatedScheduledExecutorService
            (new ScheduledThreadPoolExecutor(1));
    }

//创建只有一个线程的定时任务,自己管理线程的创建
public static ScheduledExecutorService newSingleThreadScheduledExecutor(ThreadFactory threadFactory) {
        return new DelegatedScheduledExecutorService
            (new ScheduledThreadPoolExecutor(1, threadFactory));
    }

//创建corePoolSize个线程的任务调度
 public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
        return new ScheduledThreadPoolExecutor(corePoolSize);
    }

//创建corePoolSize个线程的任务调度,自己管理线程的创建
public static ScheduledExecutorService newScheduledThreadPool(
            int corePoolSize, ThreadFactory threadFactory) {
        return new ScheduledThreadPoolExecutor(corePoolSize, threadFactory);
    }

//代理ExecutorService使之创建后无法更改
public static ExecutorService unconfigurableExecutorService(ExecutorService executor) {
        if (executor == null)
            throw new NullPointerException();
        return new DelegatedExecutorService(executor);
    }

//代理ScheduledExecutorService使之创建后无法更改
 public static ScheduledExecutorService unconfigurableScheduledExecutorService(ScheduledExecutorService executor) {
        if (executor == null)
            throw new NullPointerException();
        return new DelegatedScheduledExecutorService(executor);
    }

//返回默认的线程创建管理
 public static ThreadFactory defaultThreadFactory() {
        return new DefaultThreadFactory();
    }

//返回与当前线程具有相同权限的线程管理
public static ThreadFactory privilegedThreadFactory() {
        return new PrivilegedThreadFactory();
    }

//将Runnable装饰为Callable
 public static <T> Callable<T> callable(Runnable task, T result) {
        if (task == null)
            throw new NullPointerException();
        return new RunnableAdapter<T>(task, result);
    }


 public static Callable<Object> callable(Runnable task) {
        if (task == null)
            throw new NullPointerException();
        return new RunnableAdapter<Object>(task, null);
    }

//装饰特权对象
  public static Callable<Object> callable(final PrivilegedAction<?> action) {
        if (action == null)
            throw new NullPointerException();
        return new Callable<Object>() {
            public Object call() { return action.run(); }};
    }

public static Callable<Object> callable(final PrivilegedExceptionAction<?> action) {
        if (action == null)
            throw new NullPointerException();
        return new Callable<Object>() {
            public Object call() throws Exception { return action.run(); }};
    }

public static <T> Callable<T> privilegedCallable(Callable<T> callable) {
        if (callable == null)
            throw new NullPointerException();
        return new PrivilegedCallable<T>(callable);
    }

public static <T> Callable<T> privilegedCallableUsingCurrentClassLoader(Callable<T> callable) {
        if (callable == null)
            throw new NullPointerException();
        return new PrivilegedCallableUsingCurrentClassLoader<T>(callable);
    }


分享到:
评论

相关推荐

    Java多线程programmingShiZhanZhiNan(呵心篇).源码

    Java多线程编程实战指南(呵心篇)是一份深入探讨Java并发编程的资源,它提供了丰富的源码示例来帮助开发者理解并掌握多线程技术。在这个领域,Java提供了强大的支持,使得开发者能够构建高性能、高并发的应用程序。...

    java并发编程艺术源码-ArtConcurrentBook:JAVA并发编程的艺术

    - **读写锁**:`ReentrantReadWriteLock`允许多个读线程同时访问,但写线程独占资源,提高了并发性能。 3. **并发工具类** - **Future和Callable**:`Future`代表异步计算的结果,`Callable`定义了计算任务,它们...

    Java 7编程高级进阶源代码

    此外,还新增了Executors框架的改进,比如支持定时和周期性任务的ScheduledExecutorService,使得管理线程和调度任务更加灵活和高效。 在类型推断方面,Java 7引入了钻石操作符(),简化了匿名内部类和泛型实例化...

    BATJ面试题汇总及详解(进大厂必看)

    事务隔离级别包括读未提交、读已提交、可重复读、串行化。分布式事务如2PC(两阶段提交)、3PC(三阶段提交)解决了跨数据库的事务一致性问题。 【Redis】 Redis速度快的原因包括单线程模型、内存存储、高效的数据...

    大厂必学BAT面试题汇总及详解

    事务的隔离级别有读未提交、读已提交、可重复读、串行化。MySQL的binlog有ROW、STATEMENT、MIXED三种模式,各有优缺点。分布式事务解决方案如2PC、3PC。数据库事务隔离级别决定了并发操作的可见性和一致性,MySQL...

    20-BAT面试题汇总及详解(进大厂必看).docx

    创建线程池的方式主要有使用`Executors`的静态工厂方法,如`newFixedThreadPool`, `newSingleThreadExecutor`, `newWorkStealingPool`等。线程生命周期包括新建、就绪、运行、等待、阻塞和终止等状态,当线程无任务...

    Bat面试题汇总&详解

    事务隔离级别有读未提交、读已提交、可重复读、串行化。MySQL的binlog模式有ROW、STATEMENT、MIXED。分布式事务解决方案如两阶段提交、三阶段提交等。SQL解析过程涉及词法分析、语法分析、优化器和执行器。 【Redis...

    Java Concurrency的一些资料

    - **CopyOnWriteArrayList** 和 **CopyOnWriteArraySet**:适用于读多写少的场景,写操作时会复制整个数组,保证并发读取不会受影响。 - **BlockingQueue**:用于线程间的数据传递,支持阻塞式操作。 5. **原子...

    Java AtomicInteger类的使用方法详解

    final ExecutorService exec = Executors.newFixedThreadPool(5); final File root = new File("D:\\ISO"); // 完成标志 final File exitFile = new File(""); // 原子整型,读个数 final AtomicInteger rc = ...

    Java Concurrency In Practice Learning Note

    - Executor框架:通过ExecutorService、ThreadPoolExecutor和Executors等类管理线程池,实现任务调度。 - BlockingQueue:线程安全的数据结构,用于线程间数据传递,如ArrayBlockingQueue、LinkedBlockingQueue等...

    api-http-utils:支持同步和异步

    在并发方面,`api-http-utils-master`中的源码可能包含对线程池的管理,如自定义ThreadPoolExecutor或使用Executors提供的固定大小线程池,以控制并发程度和避免资源过度消耗。此外,库可能还考虑了线程安全问题,...

Global site tag (gtag.js) - Google Analytics