多线程之四大线程池的使用介绍

来源:互联网 发布:网络教育能考公务员吗 编辑:程序博客网 时间:2024/06/05 11:38

线程池的简介

线程池简单的说就是管理线程的一个总调度官,它可以存储着多个核心线程和多个非核心线程,也可以派遣核心线程或非核心线程去处理事情,总之它就是线程的老大,当然它也有四个小弟,即是四大线程池

一.提到线程池,当然也要介绍一下线程池的优点:

  1. 重用线程池中的线程,避免因为线程的创建和销毁所带来的性能开销

  2. 能有效控制线程池的最大并发数,避免大量的线程之间因互相抢占系统资源而导致的阻塞现象

  3. 能够对线程简单的管理,并提供定时执行以及指定间隔循环执行等功能

二.真正的线程池只有一个类:ThreadPoolExecutor,而四大线程池则是不同功能的线程池,其关系如下图:

三.线程池采用的是工厂模式,其四大线程池就是由老大打造出来的:

ExecutorService fixedThreadExecutor = Executors.newFixedThreadPool(5);ExecutorService cachedThreadExecutor = Executors.newCachedThreadPool();ExecutorService scheduledThreadExecutor = Executors.newScheduledThreadPool(5);ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();

四.一般一个简单线程池至少包含下列组成部分:

线程池管理器(ThreadPoolManager):用于创建并管理线程池工作线程(WorkThread):线程池中线程任务接口(Task):每个任务必须实现的接口,以供工作线程调度任务的执行任务队列:用于存放没有处理的任务,提供一种缓冲机制

Java里面线程池的顶级接口是Executor,但是严格意义上讲Executor并不是一个线程池,而只是一个执行线程的工具,真正的线程池接口是ExecutorService

四大线程池之老大ThreadPoolExecutor

ThreadPoolExecutor是线程池的真正实现,它的构造方法提供了一系列参数来配置线程池,其构造方法如下:

public ThreadPoolExecutor(int corePoolSize,int maximumPoolSize,long keepAliveTime,TimeUnit unit,BlockingQueue<Runnable> workQueue,ThreadFactory threadFactory,RejectedExecutionHandler handler)

四大线程池之FixedThreadPool

FixedThreadPool是一种线程数量固定的线程池,当线程处于空闲状态时,它们并不会被回收,除非线程池被关闭了,其特点是:

1.线程数量固定  2.空闲不会被回收3.更快的响应速度  4.无超时机制,无大小限制

FixedThreadPool的基本使用(我们创建五个线程的线程池来演示):

public class Test1 {    public static void main(String[] args) throws Exception{        ExecutorService fixedThreadExecutor = Executors.newFixedThreadPool(5);        for(int i=0;i<10;i++){            MyRunnable myRunnable = new MyRunnable(i);            fixedThreadExecutor.execute(myRunnable);        }    }}class MyRunnable implements Runnable{//模拟耗时任务    int threadNum;    public MyRunnable(int threadNum){        this.threadNum=threadNum;    }    @Override    public void run() {        System.out.println("打印信息:执行第"+threadNum+"号线程");        try{            Thread.currentThread().sleep(4000);        }catch(InterruptedException e){            e.printStackTrace();        }        System.out.println("打印信息:完成第"+threadNum+"号线程");    }}

四大线程池之CachedThreadPool

CachedThreadPool是一种线程数量不定的线程池,它只有非核心线程,并且其最大线程数为Integer.MAX_VALUE,其特点是:

1.只有最大线程数为int的最大值  2.超时时间为603.当整个线程池都出去闲置状态超过60秒的时候,会被回收  4.几乎不占用任何系统资源
public class Test1 {    public static void main(String[] args) throws Exception{        ExecutorService cachedThreadExecutor = Executors.newCachedThreadPool();        for(int i=0;i<10;i++){            MyRunnable myRunnable = new MyRunnable(i);            cachedThreadExecutor.execute(myRunnable);        }    }}class MyRunnable implements Runnable{//模拟耗时任务    int threadNum;    public MyRunnable(int threadNum){        this.threadNum=threadNum;    }    @Override    public void run() {        System.out.println("打印信息:执行第"+threadNum+"号线程");        try{            Thread.currentThread().sleep(4000);        }catch(InterruptedException e){            e.printStackTrace();        }        System.out.println("打印信息:完成第"+threadNum+"号线程");    }}

四大线程池之ScheduledThreadPool

ScheduledThreadPool是一种核心线程数量时固定的,而非核心线程数是没有限制的,并且当非核心线程闲置时会被立即回收,其特点是:

1.核心数量固定  2.非核心数量无限制  3.非核心线程闲置时立即被回收
public class Test1 {    public static void main(String[] args) throws Exception{         ExecutorService scheduledThreadExecutor = Executors.newScheduledThreadPool(5);        for(int i=0;i<10;i++){            MyRunnable myRunnable = new MyRunnable(i);            scheduledThreadExecutor.execute(myRunnable);        }    }}class MyRunnable implements Runnable{//模拟耗时任务    int threadNum;    public MyRunnable(int threadNum){        this.threadNum=threadNum;    }    @Override    public void run() {        System.out.println("打印信息:执行第"+threadNum+"号线程");        try{            Thread.currentThread().sleep(4000);        }catch(InterruptedException e){            e.printStackTrace();        }        System.out.println("打印信息:完成第"+threadNum+"号线程");    }}

四大线程池之SingleThreadExecutor

SingleThreadExecutor是一种只有一个核心线程的线程池,它确保所有的任务都在同一个线程中按顺序执行,其特点是:

1.只有一个核心线程
public class Test1 {    public static void main(String[] args) throws Exception{        ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();        for(int i=0;i<10;i++){            MyRunnable myRunnable = new MyRunnable(i);            singleThreadExecutor.execute(myRunnable);        }    }}class MyRunnable implements Runnable{//模拟耗时任务    int threadNum;    public MyRunnable(int threadNum){        this.threadNum=threadNum;    }    @Override    public void run() {        System.out.println("打印信息:执行第"+threadNum+"号线程");        try{            Thread.currentThread().sleep(4000);        }catch(InterruptedException e){            e.printStackTrace();        }        System.out.println("打印信息:完成第"+threadNum+"号线程");    }}

最后对四大线程池的总结

FixedThreadPool:核心固定,非核心为0  CachedThreadPool:核心为0,非核心无限制ScheduledThreadPool:核心固定,非核心无限制  SingleThreadExecutor:核心为1,非核心为0
0 0