线程池的好处和实现

Java 实现Demo

package com.logan;


import java.util.concurrent.*;

/**
 * 线程池测试类
 * @author logan
 * @Date 2019年05月08日23:40:49
 */
public class MyThreadTester {

    public static void main(String[] args) {

        // 获取自定义线程池
        MyThreadPool myThreadPool = MyThreadPool.getThreadPool();

        //执行
        myThreadPool.execute(new MyThread());

        //关闭资源
        myThreadPool.close();

    }

}

/**
 * 自定义线程池
 *
 * 线程池的好处
 * 1.重用-避免线程的重复创建和销毁。这是很大的开销。
 * 2.控制并发数-线程池可以控制并发数。以达到对公共资源访问进行限流,从而达到削峰的效果。
 * 3.线程池可以批量管理,比方说可以定时、定期的执行一些任务等(e.g. Executors.newScheduledThreadPool)。
 *
 */
class MyThreadPool {

    private int corePoolSize;
    private int maxNumPoolSize;
    private long keepAliveTime;

    private static MyThreadPool theThreadPool;
    private ThreadPoolExecutor threadPoolExecutor;

    /**
     *
     * @param corePoolSize 处理器个数
     * @param maxNumPoolSize 最大线程个数
     * @param keepAliveTime 存活时间 单位:秒
     */
    private MyThreadPool(int corePoolSize, int maxNumPoolSize, long keepAliveTime){
        this.corePoolSize = corePoolSize;
        this.maxNumPoolSize = maxNumPoolSize;
        this.keepAliveTime = keepAliveTime;
    }

    public void execute(Runnable r){

        if(r == null)
            return ;
        if(threadPoolExecutor == null){
            threadPoolExecutor = new ThreadPoolExecutor(
                    this.corePoolSize, //
                    maxNumPoolSize, // 最大线程个数
                    keepAliveTime, // 存活时间
                    TimeUnit.SECONDS, // 存活时间单位
                    new LinkedBlockingDeque<Runnable>(), // 阻塞双端队列,方便于从头尾,插入读取数据
                    Executors.defaultThreadFactory(), // thread工厂,默认工厂就是一个起名
                    new ThreadPoolExecutor.AbortPolicy()//拒绝策略,回绝并抛出RejectedExecutionException
                    );
        }

        threadPoolExecutor.execute(r);

    }

    public static MyThreadPool getThreadPool(){
        // 单例
        if(theThreadPool == null){
            synchronized (MyThreadPool.class){
                if(theThreadPool == null){
                    // 获取处理器个数
                    int maxCoreSize = Runtime.getRuntime().availableProcessors();
                    int maxThreadNum = maxCoreSize * 2 + 1; // 计算合理并发个数
                    theThreadPool = new MyThreadPool(maxCoreSize,maxThreadNum,0L);
                }
            }
        }
        return theThreadPool;
    }

    /**
     * 关闭资源
     */
    public void close(){
        if(threadPoolExecutor != null){
            threadPoolExecutor.shutdown();
        }
    }

}

class MyThread implements Runnable{
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + " Hello, Logan!!!");
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值