线程池 种类

fixed single cached

import java.util.concurrent.*;

public class T {

    public static void main(String[] args) {
        ExecutorService threadPool = getFixedThreadPool();
        for (int i = 0; i < 8; i++) {
            threadPool.submit(new MyTask(i));
        }
    }

    /*
     new ThreadPoolExecutor(nThreads, nThreads,
                            0L, TimeUnit.MILLISECONDS,
                            new LinkedBlockingQueue<Runnable>())
    适用任务量已知,相对耗时的任务
    pool-1-thread-【1】...0
    pool-1-thread-【3】...2
    pool-1-thread-【2】...1
    pool-1-thread-【2】...3
    pool-1-thread-【3】...4
    pool-1-thread-【1】...5
    pool-1-thread-【1】...7
    pool-1-thread-【3】...6
    * */
    public static ExecutorService getFixedThreadPool() {
        ExecutorService threadPool = Executors.newFixedThreadPool(3);
        return threadPool;
    }


    /*
    new ThreadPoolExecutor(1, 1,
                           0L, TimeUnit.MILLISECONDS,
                           new LinkedBlockingQueue<Runnable>())
    来一个任务执行一个任务
    适用于按照顺序执行的任务

    pool-1-thread-【1】...(0)
    pool-1-thread-【1】...(1)
    pool-1-thread-【1】...(2)
    pool-1-thread-【1】...(3)
    pool-1-thread-【1】...(4)
    pool-1-thread-【1】...(5)
    pool-1-thread-【1】...(6)
    pool-1-thread-【1】...(7)
    * */
    public static ExecutorService getSingleThreadPool() {
        ExecutorService threadPool = Executors.newSingleThreadExecutor();
        return threadPool;
    }

    /*
    new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                           60L, TimeUnit.SECONDS,
                           new SynchronousQueue<Runnable>());
    创建大量的线程
    适用:任务数比较密集,每个任务执行比较短的情况

    pool-1-thread-【1】...0
    pool-1-thread-【6】...5
    pool-1-thread-【7】...6
    pool-1-thread-【5】...4
    pool-1-thread-【4】...3
    pool-1-thread-【3】...2
    pool-1-thread-【2】...1
    pool-1-thread-【8】...7
    * */
    public static ExecutorService getCachedThreadPool() {
        ExecutorService threadPool = Executors.newCachedThreadPool();
        return threadPool;
    }
}

class MyTask implements Runnable {
    private int i;

    public MyTask(int i) {
        this.i = i;
    }

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + "..." + i);
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

delayed

import lombok.SneakyThrows;

import java.util.Date;
import java.util.concurrent.*;

/*
main发起线程池提交时间Thu Aug 15 15:12:20 CST 2024
pool-1-thread-2 开始时间Thu Aug 15 15:12:20 CST 2024和发起线程池提交时间差=0s
pool-1-thread-2 结束时间Thu Aug 15 15:12:21 CST 2024
pool-1-thread-1 开始时间Thu Aug 15 15:12:22 CST 2024和发起线程池提交时间差=2s
pool-1-thread-1 结束时间Thu Aug 15 15:12:23 CST 2024
pool-1-thread-2 开始时间Thu Aug 15 15:12:25 CST 2024和发起线程池提交时间差=5s
pool-1-thread-2 结束时间Thu Aug 15 15:12:26 CST 2024
* */
public class T {

    @SneakyThrows
    public static void main(String[] args) {
        ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(2);

        Date submitDate = new Date();
        System.out.println(Thread.currentThread().getName() + "发起线程池提交时间" + submitDate);
        scheduledThreadPool.schedule(new MyTask(submitDate), 0, TimeUnit.SECONDS);
        scheduledThreadPool.schedule(new MyTask(submitDate), 2, TimeUnit.SECONDS);
        scheduledThreadPool.schedule(new MyTask(submitDate), 5, TimeUnit.SECONDS);

        Thread.sleep(8000);
        scheduledThreadPool.shutdownNow();

    }

}

class MyTask implements Runnable {

    private Date date;

    public MyTask(Date date) {
        this.date = date;
    }

    @Override
    public void run() {
        try {
            Date dateTemp = new Date();
            System.out.println(Thread.currentThread().getName() +
                    " 开始时间" + dateTemp
                    + "和发起线程池提交时间差=" + (dateTemp.getTime() - this.date.getTime()) / 1000 + "s");
            Thread.sleep(1000);
            System.out.println(Thread.currentThread().getName() + " 结束时间" + new Date());
        } catch (Exception e) {

        }
    }
}
posted @ 2024-08-15 15:13  干饭达人GoodLucy  阅读(1)  评论(0编辑  收藏  举报