1、先创建线程池

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class CustomThreadPool {
    /**
     * 线程池的基本大小
     */
    static int corePoolSize = 40;
    /**
     * 线程池最大数量
     */
    static int maximumPoolSizeSize = 40;
    /**
     * 线程活动保持时间
     */
    static long keepAliveTime = 1;
    /**
     * 任务队列
     */
    static ArrayBlockingQueue workQueue = new ArrayBlockingQueue(10);
    
    public void saveMessage(Runnable task){
        ThreadPoolExecutor pool = new ThreadPoolExecutor(corePoolSize, maximumPoolSizeSize, keepAliveTime, TimeUnit.MICROSECONDS, workQueue);
        pool.execute(task);
    }
}

2、创建线程进行调用

Runnable runnable = new Runnable() {
  @Override
  public void run() {
    //线程需要处理的逻辑
  }
 };
new CustomThreadPool().saveMessage(runnable);

 

posted on 2019-07-26 14:54  程序员丁先生  阅读(107)  评论(0编辑  收藏  举报