为什么要用线程池它的优势以及如何使用,也是实现多线程的第四种方式

    线程池做的工作主要是控制运行的线程数量,处理过程中将任务放入队列,然后在线程创建后启动这些任务,如果线程数量超过了最大数量超出数量的线程排队等候,等其他线程执行完毕,再从队列中取出任务来执行,它的主要特点是,线程复用,控制最大并发数,管理线程。线程池的优势有以下三点。

  •     降低资源消耗,通过重复利用已创建的线程降低线程创建和销毁造成的消耗。
  •     提高响应速度,当任务到达时,任务可以不需要的等到线程创建就能立即执行。
  •     提高线程的可管理性,线程是稀缺资源,如果无限制的创建,不仅会消耗系统资源,还会降低系统的稳定性,使用线程池可以进行统一的分配,调优和监控。

    那java中是怎样实现的线程池呢?是通过Executor框架实现的,该框架中用到了Executor,Executors,ExecutorService,ThreadPoolExecutor这几个接口或类,它们都是JUC包下的。

    java.util.concurrent.Executors类是Executor的辅助类,类似于java中操作数组的辅助类java.util.Arrays,以及操作集合的java.util.Collections类,接下来我们讲讲Executors类中的主要三个方法。

  • Executors#newFixedThreadPool

    创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中的等待,它创建的线程池corePoolSize和maximnumPoolSize是相等的,它使用的是LinkedBlockingQueue,我们来看下源码。

    public static ExecutorService newFixedThreadPool(int nThreads) {
        return new ThreadPoolExecutor(nThreads, nThreads,
                                      0L, TimeUnit.MILLISECONDS,
                                      new LinkedBlockingQueue<Runnable>());
    }

    我们创建一个5个线程的线程池,来模仿银行办理业务的场景,5个线程相当于5个办理窗口

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

public class MyThreadPoolDemo {

    /**
     * @param args
     */
    public static void main(String[] args) {

        ExecutorService threadPool = Executors.newFixedThreadPool(5); // 一池5个线程

        try {
            for (int i = 0; i < 10; i++) {
                threadPool.execute(() -> {
                    System.err.println(Thread.currentThread().getName() + "\t办理业务");
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            threadPool.shutdown();
        }
    }

}

执行结果

pool-1-thread-1    办理业务
pool-1-thread-3    办理业务
pool-1-thread-2    办理业务
pool-1-thread-1    办理业务
pool-1-thread-1    办理业务
pool-1-thread-1    办理业务
pool-1-thread-3    办理业务
pool-1-thread-1    办理业务
pool-1-thread-5    办理业务
pool-1-thread-4    办理业务

    从执行结果来看,5个线程(银行办理窗口)分别办理业务,我们接着往下看。

  • Executors#newSingleThreadExecutor

    创建一个单线程化的线程池,它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序执行,它将corePoolSize和maximnumPoolSize都设置为1,它也使用的是LinkedBlockingQueue,我们来看下源码。

    public static ExecutorService newSingleThreadExecutor() {
        return new FinalizableDelegatedExecutorService
            (new ThreadPoolExecutor(1, 1,
                                    0L, TimeUnit.MILLISECONDS,
                                    new LinkedBlockingQueue<Runnable>()));
    }

     创建只有一个线程的线程池。

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

public class MyThreadPoolDemo {

    /**
     * @param args
     */
    public static void main(String[] args) {


        ExecutorService threadPool = Executors.newSingleThreadExecutor(); // 一池1个线程

        try {
            for (int i = 0; i < 10; i++) {
                threadPool.execute(() -> {
                    System.err.println(Thread.currentThread().getName() + "\t办理业务");
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            threadPool.shutdown();
        }
    }

}

执行结果

pool-1-thread-1    办理业务
pool-1-thread-1    办理业务
pool-1-thread-1    办理业务
pool-1-thread-1    办理业务
pool-1-thread-1    办理业务
pool-1-thread-1    办理业务
pool-1-thread-1    办理业务
pool-1-thread-1    办理业务
pool-1-thread-1    办理业务
pool-1-thread-1    办理业务

    从执行结果来看,全程只有一个线程(银行办理业务窗口)在办理业务。

  • Executors#newCachedThreadPool

    创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程。,它将corePoolSize设置为0,将maximnumPoolSize设置为Integer.MAX_VALUE,它使用的是SynchronousQueue,也就是说来了任务就创建线程运行,当前线程空闲超过60秒,就销毁线程,我们来看下源码。

    public static ExecutorService newCachedThreadPool() {
        return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                      60L, TimeUnit.SECONDS,
                                      new SynchronousQueue<Runnable>());
    }

    创建有N个线程的线程池。

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

public class MyThreadPoolDemo {

    /**
     * @param args
     */
    public static void main(String[] args) {


        ExecutorService threadPool = Executors.newCachedThreadPool(); // 一池N个线程


        try {
            for (int i = 0; i < 10; i++) {
                threadPool.execute(() -> {
                    System.err.println(Thread.currentThread().getName() + "\t办理业务");
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            threadPool.shutdown();
        }
    }

}

执行结果

pool-1-thread-2    办理业务
pool-1-thread-6    办理业务
pool-1-thread-10    办理业务
pool-1-thread-4    办理业务
pool-1-thread-8    办理业务
pool-1-thread-3    办理业务
pool-1-thread-7    办理业务
pool-1-thread-1    办理业务
pool-1-thread-5    办理业务
pool-1-thread-9    办理业务

    此种线程池创建线程非常灵活,会根据处理业务的速度,自动为我们创建线程,回收线程等。前面的文章我们有聊到使用多线程的三种方式,继承Thread类,实现Runnable接口,实现Callable接口,那实现多线程的第四种方式就是使用线程池,线程池在大厂面试中,也是必问的面试题,我们要好好的来掌握它,那今天基本用法就介绍到这里,后面文章会聊聊线程池工作原理以及线程池核心的7大参数,下篇见。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值