多线程和并发库应用八-线程池

19 篇文章 0 订阅
15 篇文章 0 订阅

Java通过Executors提供几种线程池,主要有:
newCachedThreadPool创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程。
newFixedThreadPool 创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待。
newSingleThreadExecutor 创建一个单线程化的线程池,它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序(FIFO, LIFO, 优先级)执行。
newScheduledThreadPool 创建一个定长线程池,支持定时及周期性任务执行。

1.newFixedThreadPool
主要创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待 可以在输出中查看

public class Main {
    public static void main(String[] args){
        final ExecutorService threadPool=Executors.newFixedThreadPool(3);
        for (int i=1;i<=10;i++){
            final  int task=i;
            threadPool.execute(new Runnable() {
                public void run() {
                    for (int j=1;j<=10;j++){
                        try {
                            Thread.sleep(20);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println(Thread.currentThread().getName()+" task id="+task+"  job id="+j);
                    }
                }
            });
        }
        System.out.println("task all commit");
        threadPool.shutdown();
    }
}

2.newCachedThreadPool
主要用于动态创建线程池中的个数

public class Main1 {
    public static void main(String[] args){
        final ExecutorService threadPool=Executors.newCachedThreadPool();
        for (int i=1;i<=10;i++){
            final  int task=i;
            threadPool.execute(new Runnable() {
                public void run() {
                    for (int j=1;j<=10;j++){
                        try {
                            Thread.sleep(20);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println(Thread.currentThread().getName()+" task id="+task+"  job id="+j);
                    }
                }
            });
        }
        System.out.println("task all commit");
        threadPool.shutdown();
    }
}

3.newSingleThreadExecutor 创建只有一个线程的线程池 当线程池中的线程die 了 会重新创建一个线程

public class Main2 {
    public static void main(String[] args){
        final ExecutorService threadPool=Executors.newSingleThreadExecutor();
        for (int i=1;i<=10;i++){
            final  int task=i;
            threadPool.execute(new Runnable() {
                public void run() {
                    for (int j=1;j<=10;j++){
                        try {
                            Thread.sleep(20);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println(Thread.currentThread().getName()+" task id="+task+"  job id="+j);
                    }
                }
            });
        }
        System.out.println("task all commit");
        threadPool.shutdown();
    }
}

4.newScheduledThreadPool

public class Main4 {
    public static void main(String[] args){

        Executors.newScheduledThreadPool(3).scheduleAtFixedRate(new Runnable() {
            public void run() {
             System.out.print("booming");
            }
        },5,2,TimeUnit.SECONDS);
    }
}

5.shutdown 和 shutdown now 的区别
可以关闭 ExecutorService,这将导致其拒绝新任务。提供两个方法来关闭 ExecutorService。shutdown() 方法在终止前允许执行以前提交的任务,而 shutdownNow() 方法阻止等待任务启动并试图停止当前正在执行的任务。在终止时,执行程序没有任务在执行,也没有任务在等待执行,并且无法提交新任务。应该关闭未使用的 ExecutorService 以允许回收其资源。
文章地址:http://www.haha174.top/article/details/254309
项目源码:https://github.com/haha174/thread-learning.git

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值