您好,登录后才能下订单哦!
我们之前使用线程的时候都是使用new Thread来进行线程的创建,但是这样会有一些问题。如:
相比new Thread,Java提供的四种线程池的好处在于:
java.util.concurrent.Executor 负责线程的使用和调度的根接口
        |--ExecutorService 子接口: 线程池的主要接口
                |--ThreadPoolExecutor 线程池的实现类
                |--ScheduledExceutorService 子接口: 负责线程的调度
                    |--ScheduledThreadPoolExecutor : 继承ThreadPoolExecutor,实现了ScheduledExecutorServiceExecutorService newFixedThreadPool() : 创建固定大小的线程池
ExecutorService newCachedThreadPool() : 缓存线程池,线程池的数量不固定,可以根据需求自动的更改数量。
ExecutorService newSingleThreadExecutor() : 创建单个线程池。 线程池中只有一个线程
ScheduledExecutorService newScheduledThreadPool() : 创建固定大小的线程,可以延迟或定时的执行任务
创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程。
/**
 * 创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程。
 * 线程池为无限大,当执行第二个任务时第一个任务已经完成,会复用执行第一个任务的线程,而不用每次新建线程。
 *
 * @throws InterruptedException
 */
public static void cachedThreadPool() throws InterruptedException {
    ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
    for(int i = 0; i < 10; i++) {
        final int index = i;
        // 加上这一行代码,让前面的线程执行完成,可以看出,一直用的都是同一个线程,没有新建
        TimeUnit.SECONDS.sleep(1);
        cachedThreadPool.execute(() - > {
            System.out.println(Thread.currentThread() + "====" + index);
        });
    }
}加上TimeUnit.SECONDS.sleep(1);执行结果:
一直复用的是同一个线程
Thread[pool-1-thread-1,5,main]====0
Thread[pool-1-thread-1,5,main]====1
Thread[pool-1-thread-1,5,main]====2
Thread[pool-1-thread-1,5,main]====3
Thread[pool-1-thread-1,5,main]====4
Thread[pool-1-thread-1,5,main]====5
Thread[pool-1-thread-1,5,main]====6
Thread[pool-1-thread-1,5,main]====7
Thread[pool-1-thread-1,5,main]====8
Thread[pool-1-thread-1,5,main]====9去掉TimeUnit.SECONDS.sleep(1);执行结果:
创建了10个不同的线程
 Thread[pool-1-thread-1,5,main]====0
Thread[pool-1-thread-2,5,main]====1
Thread[pool-1-thread-3,5,main]====2
Thread[pool-1-thread-4,5,main]====3
Thread[pool-1-thread-5,5,main]====4
Thread[pool-1-thread-6,5,main]====5
Thread[pool-1-thread-7,5,main]====6
Thread[pool-1-thread-9,5,main]====8
Thread[pool-1-thread-8,5,main]====7
Thread[pool-1-thread-10,5,main]====9创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待。
/**
 * 创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待。
 * 本例中,因为线程池大小为3,每个任务输出index后sleep 2秒,所以每两秒打印3个数字。
 */
public static void fixedThreadPool() {
    ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3);
    for(int i = 0; i < 10; i++) {
        final int index = i;
        fixedThreadPool.execute(() - > {
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch(InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread() + "====" + index);
        });
    }
}执行结果:
因为线程池大小为3,只会创建3个线程,每个任务输出index后sleep 2秒,所以每两秒打印3个数字。
Thread[pool-1-thread-1,5,main]====0
Thread[pool-1-thread-3,5,main]====2
Thread[pool-1-thread-2,5,main]====1
Thread[pool-1-thread-1,5,main]====3
Thread[pool-1-thread-3,5,main]====4
Thread[pool-1-thread-2,5,main]====5
Thread[pool-1-thread-1,5,main]====6
Thread[pool-1-thread-3,5,main]====7
Thread[pool-1-thread-2,5,main]====8
Thread[pool-1-thread-1,5,main]====9创建一个定长线程池,支持定时及周期性任务执行。
/**
 * 创建一个定长线程池,延迟1秒,每隔1秒周期性任务执行
 */
public static void scheduledThreadPool() {
    ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(2);
    scheduledThreadPool.scheduleAtFixedRate(() - > {
        System.out.println("scheduledThreadPool执行中...");
    }, 1, 1, TimeUnit.SECONDS);
}执行结果:
scheduledThreadPool执行中...
scheduledThreadPool执行中...
......
scheduledThreadPool执行中...
scheduledThreadPool执行中...
......创建一个单线程化的线程池,它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序(FIFO, LIFO, 优先级)执行。
/**
 * 创建一个单线程化的线程池,它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序(FIFO, LIFO, 优先级)执行
 */
public static void singleThreadExecutor() {
    ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
    for(int i = 0; i < 10; i++) {
        final int index = i;
        singleThreadExecutor.execute(() - > {
            System.out.println(Thread.currentThread() + "====" + index);
        });
    }
}执行结果:
只会创建一个线程
Thread[pool-1-thread-1,5,main]====0
Thread[pool-1-thread-1,5,main]====1
Thread[pool-1-thread-1,5,main]====2
Thread[pool-1-thread-1,5,main]====3
Thread[pool-1-thread-1,5,main]====4
Thread[pool-1-thread-1,5,main]====5
Thread[pool-1-thread-1,5,main]====6
Thread[pool-1-thread-1,5,main]====7
Thread[pool-1-thread-1,5,main]====8
Thread[pool-1-thread-1,5,main]====9
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。