其他分享
首页 > 其他分享> > 创建线程池的方式有哪几种?

创建线程池的方式有哪几种?

作者:互联网

创建线程池的方式有哪几种?

  1. 线程创建方式有哪几种?
  2. 什么是定长线程池?
  3. 什么是可缓存线程池?
//定长线程池
//解释:每当提交一个任务就创建一个线程,直到达到线程池的最大数量,这时线程数量不在变化,当线程发生错误结束时,线程池会补充一个新的线程
static ExecutorService fixedPool = Executors.newFixedThreadPool(3);
//定长可执行周期任务的线程池
static ScheduledExecutorService scheduledPool = Executors.newScheduledThreadPool(3);
//缓存线程池
//解释:如果线程池的容量超过了任务数,会自动回收空闲线程,任务增加时可以自动添加新线程,线程池的容量不限制
static ExecutorService cachedPool = Executors.newCachedThreadPool();
//单例线程池
//解释:线程异常结束,会创建一个新的线程,能确保任务按提交顺序执行
static ExecutorService singlePool = Executors.newSingleThreadExecutor();
//单例可执行周期任务的线程池
static ScheduledExecutorService singleScheduledPool = Executors.newSingleThreadScheduledExecutor();
//窃取线程池
//解释:不保证执行顺序,适合任务耗时差异较大
static ExecutorService workPool = Executors.newWorkStealingPool();

线程池创建方式有以上六种,下面来看看实例代码

package cn.zwolf.app.controller;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class ThreadPoolTest {
    
    //定长线程池
    static ExecutorService fixedPool  = Executors.newFixedThreadPool(3);
    //定长可执行周期任务的线程池
    static ScheduledExecutorService scheduledPool = Executors.newScheduledThreadPool(3);
    //缓存线程池
    static ExecutorService cachedPool  = Executors.newCachedThreadPool();
    //单例线程池
    static ExecutorService singlePool = Executors.newSingleThreadExecutor();
    //单例可执行周期任务的线程池
    static ScheduledExecutorService singleScheduledPool = Executors.newSingleThreadScheduledExecutor();
    //窃取线程池
    static ExecutorService workPool = Executors.newWorkStealingPool();

    public static void main(String[] args) {
        //定长线程池
        fixedThreadPool();
        //定长可执行周期任务的线程池
        scheduledThreadPool();
        //缓存线程池
        cachedThreadPool();
        //单例线程池
        singleThreadPool();
        //单例可执行周期任务的线程池
        singlnScheduledThreadPool();
        //窃取线程池
        workThreadPool();
    }
    
	//测试定长线程池,线程池的容量为3,提交6个任务,根据打印结果可以看出先执行前3个任务,3个任务结束后在执行后面的任务
    private static void fixedThreadPool() {
        threadPool(fixedPool );
        fixedPool .shutdown();
    }

    private static void scheduledThreadPool() {
        sThreadPool(scheduledPool);
        scheduledPool.shutdown();
    }

    private static void cachedThreadPool() {
        threadPool(cachedPool);
        cachedPool .shutdown();
    }

    private static void singleThreadPool() {
        threadPool(singlePool);
        singlePool .shutdown();
    }

    private static void singlnScheduledThreadPool() {
        sThreadPool(singleScheduledPool);
        singleScheduledPool.shutdown();
    }

    private static void workThreadPool() {
        threadPool(workPool);
        workPool .shutdown();
    }

    private static void threadPool(ExecutorService e) {
        for (int i = 0; i < 6; i++) {
            final int index = i;
            e.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(3000);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + " index : " + index);
                }
            });
        }

        try {
            Thread.sleep(4000);
        } catch (Exception es) {
            es.printStackTrace();
        }
        System.out.println("4秒后~");
    }

    private static void sThreadPool(ScheduledExecutorService e) {
        for (int i = 0; i < 10; i++) {
            final int index = i;
            //scheduleWithFixedDelay =》 固定的延迟时间执行任务; 
            //scheduleAtFixedRate =》 固定的频率执行任务;
            e.scheduleWithFixedDelay(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName() + " index : " + index);
                }
            },0,3, TimeUnit.SECONDS);
        }
        try {
            Thread.sleep(4000);
        } catch (Exception es) {
            es.printStackTrace();
        }
        System.out.println("4秒后~");
    }
}

看完这篇文章和实践操作之后,对上面的问答题是否已经有了清楚的了解了和认识了呢?

标签:Executors,创建,void,哪几种,线程,private,static,ExecutorService
来源: https://blog.csdn.net/ZhaoCyChina/article/details/123085941