【并发编程】- 线程池使用方法allowCoreThreadTimeOut设置核心线程超时

225 阅读1分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第25天,点击查看活动详情

线程池使用方法prestartCoreThread()和prestartAllCoreThreads()启动核心线程

方法prestartCoreThread()每调用一次就创建一个核心线程,返回值为boolean,就是是否启动了。

方法prestartAllCoreThreads()的作用是启动全部核心线程,返回值是启动核心线程的数量。

执行代码如下:

public class PrestartCoreThreadRun {
    public static void main(String[] args) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println(Thread.currentThread().getName()+"  开始时间:"+simpleDateFormat.format(new Date()));
                    Thread.sleep(4000);
                    System.out.println(Thread.currentThread().getName()+"  结束时间:"+simpleDateFormat.format(new Date()));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };

        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2, 2, 6, TimeUnit.SECONDS, new LinkedBlockingQueue<>());
        System.out.println("线程池中的线程数a:"+threadPoolExecutor.getPoolSize());
        System.out.println("1="+threadPoolExecutor.prestartCoreThread());
        System.out.println("线程池中的线程数b:"+threadPoolExecutor.getPoolSize());
        System.out.println("2="+threadPoolExecutor.prestartCoreThread());
        System.out.println("线程池中的线程数c:"+threadPoolExecutor.getPoolSize());
        System.out.println("3="+threadPoolExecutor.prestartCoreThread());
        System.out.println("4="+threadPoolExecutor.prestartCoreThread());
        System.out.println("5="+threadPoolExecutor.prestartCoreThread());
        System.out.println("线程池中的线程数d:"+threadPoolExecutor.getPoolSize());
    }
}

运行结果如下:

线程池中的线程数a:0
1=true
线程池中的线程数b:1
2=true
线程池中的线程数c:2
3=false
4=false
5=false
线程池中的线程数d:2

启动线程池中所有的核心线程

public class PrestartCoreThreadRun {
    public static void main(String[] args) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println(Thread.currentThread().getName()+"  开始时间:"+simpleDateFormat.format(new Date()));
                    Thread.sleep(4000);
                    System.out.println(Thread.currentThread().getName()+"  结束时间:"+simpleDateFormat.format(new Date()));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };

        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2, 2, 6, TimeUnit.SECONDS, new LinkedBlockingQueue<>());
        System.out.println("线程池中的线程数a:"+threadPoolExecutor.getPoolSize());
        System.out.println("启动核心线程数量为:"+threadPoolExecutor.prestartAllCoreThreads());
        System.out.println("线程池中的线程数b:"+threadPoolExecutor.getPoolSize());
        System.out.println("2="+threadPoolExecutor.prestartCoreThread());
        System.out.println("线程池中的线程数c:"+threadPoolExecutor.getPoolSize());
    }
}

运行结果如下:

线程池中的线程数a:0
启动核心线程数量为:2
线程池中的线程数b:2
2=false
线程池中的线程数c:2