• 欢迎光临~

Executors:线程池的工具类处理线程

开发技术 开发技术 2022-11-30 次浏览

Executors:线程池的工具类处理线程

Executors得到线程池对象的常用方法
Executors:线程池的工具类通过调用方法返回不同类型的线程池对象。

Executors:线程池的工具类处理线程

Executors的底层其实也是基于线程池的实现类ThreadPoolExecutor创建线程池对象的。

package com.itheima.d8_threadpool;

import java.util.concurrent.*;

/**
    目标:使用Executors的工具方法直接得到一个线程池对象。
 */
public class ThreadPoolDemo3 {
    public static void main(String[] args) throws Exception {
        // 1、创建固定线程数据的线程池
        ExecutorService pool = Executors.newFixedThreadPool(3);

        pool.execute(new MyRunnable());
        pool.execute(new MyRunnable());
        pool.execute(new MyRunnable());
        pool.execute(new MyRunnable()); // 已经没有多余线程了
    }
}

Executors使用可能存在的陷阱
大型并发系统环境中使用Executors如果不注意可能会出现系统风险。

Executors:线程池的工具类处理线程

Executors使用可能存在的陷阱
大型并发系统环境中使用Executors如果不注意可能会出现系统风险。

Executors:线程池的工具类处理线程

 

Executors工具类底层是基于什么方式实现的线程池对象?
线程池ExecutorService的实现类:ThreadPoolExecutor

Executors是否适合做大型互联网场景的线程池方案?
不合适。
建议使用ThreadPoolExecutor来指定线程池参数,这样可以明确线程池的运行规则,规避资源耗尽的风险。

 

程序员灯塔
转载请注明原文链接:Executors:线程池的工具类处理线程
喜欢 (0)
违法和不良信息举报电话:022-22558618 举报邮箱:dljd@tidljd.com