ThreadPoolExecutor简单使用示例

 

 

概述

在编程规范中,不建议使用Executors去创建线程池,而是推荐使用ThreadPoolExecutor。

ThreadPoolExecutor会更明确运行规则,避免资源耗尽的风险。

因为Executors返回线程池有弊端:

1)FixedThreadPool和SingleThreadPool,允许的请求队列长度为Integer.MAX_VALUE,可能会堆积大量的请求,从而导致OOM。

2)CachedThreadPool和ScheduledThreadPool,允许的创建线程数量为Integer.MAX_VALUE,可能会创建大量的线程,从而导致OOM。

构造方法

构造方法摘要
ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue)
          用给定的初始参数和默认的线程工厂及被拒绝的执行处理程序创建新的 ThreadPoolExecutor。
ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler)
          用给定的初始参数和默认的线程工厂创建新的 ThreadPoolExecutor。
ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory)
          用给定的初始参数和默认被拒绝的执行处理程序创建新的 ThreadPoolExecutor。
ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler)
          用给定的初始参数创建新的 ThreadPoolExecutor。

下面用一个例子来说明这个。

一个任务类,里面可以是具体的执行单元,这里仅是个示例:

public class Task implements Runnable{
    private int i;
    public Task(int i){this.i = i;}

    @Override
    public void run() {
        try {
            Thread.sleep(10000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("ThreadName:"+Thread.currentThread().getName()+"线程执行:"+i);
    }
}

自定义ThreadFactory:

public class MyThreadFactory implements ThreadFactory {
    /**
     * Constructs a new {@code Thread}.  Implementations may also initialize
     * priority, name, daemon status, {@code ThreadGroup}, etc.
     *
     * @param r a runnable to be executed by new thread instance
     * @return constructed thread, or {@code null} if the request to
     * create a thread is rejected
     */
    @Override
    public Thread newThread(Runnable r) {
        return new Thread(r);
    }
}

 简单使用:

private static void simple(){
        ThreadFactory namedThreadFactory = new MyThreadFactory();
        int queueCapacity = 3, corePoolSize=2, maximumPoolSize=3;
        ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue(queueCapacity);
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(corePoolSize,maximumPoolSize,10, TimeUnit.SECONDS,arrayBlockingQueue,namedThreadFactory);
        for(int i=1;i<5;i++){
                Thread thread = namedThreadFactory.newThread(new Task(i));
                threadPoolExecutor.execute(thread);
                System.out.println("i:"+i+", queueSize:"+arrayBlockingQueue.size()
                        +", poolSize:"+threadPoolExecutor.getPoolSize()
                        +", coreSize:"+threadPoolExecutor.getCorePoolSize()
                        +", maxSize:"+threadPoolExecutor.getMaximumPoolSize());

        }
        threadPoolExecutor.shutdown();
        while (true){
            if(threadPoolExecutor.isTerminated()){
                System.out.println("over");
                break;
            }
        }
        System.out.println( );
    }

使用LinkedBlockingQueue作为示例队列

private static void LinkedBlockingQueue(){
        ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
                .setNameFormat("demo-pool-%d").build();
        int queueCapacity = 3, corePoolSize=2, maximumPoolSize=3;
        LinkedBlockingQueue linkedBlockingQueue = new LinkedBlockingQueue(queueCapacity);
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(corePoolSize,maximumPoolSize,10, TimeUnit.SECONDS,linkedBlockingQueue,namedThreadFactory);
        for(int i=1;i<21;i++){
            Thread thread = namedThreadFactory.newThread(new Task(i));
            System.out.println("即将添加数据:"+i);
            threadPoolExecutor.execute(thread);
            System.out.println("i:"+i+", queueSize:"+linkedBlockingQueue.size()
                    +", poolSize:"+threadPoolExecutor.getPoolSize()
                    +", coreSize:"+threadPoolExecutor.getCorePoolSize()
                    +", maxSize:"+threadPoolExecutor.getMaximumPoolSize());
        }

看打印的日志:

即将添加数据:1
i:1, queueSize:0, poolSize:1, coreSize:2, maxSize:3
即将添加数据:2
i:2, queueSize:0, poolSize:2, coreSize:2, maxSize:3
即将添加数据:3
i:3, queueSize:1, poolSize:2, coreSize:2, maxSize:3
即将添加数据:4
i:4, queueSize:2, poolSize:2, coreSize:2, maxSize:3
即将添加数据:5
i:5, queueSize:3, poolSize:2, coreSize:2, maxSize:3
即将添加数据:6
i:6, queueSize:3, poolSize:3, coreSize:2, maxSize:3
即将添加数据:7
Exception in thread "main" java.util.concurrent.RejectedExecutionException

当添加第一个任务的时候,由于线程池空着,直接创建核心线程来处理请求;

当已经添加完两个请求,添加第三个请求的时候,核心线程数已满,则往队列里面添加,此时queueSize=1;

添加3、4、5任务,都被添加到了队列里面,此时queueSize=3;

添加6任务的时候,核心线程已满,队列已满,运行的线程数小于maximumPoolSize,那么线程池再处理一个任务,此时poolSize=3,线程池的任务满了;

添加7任务的时候,由于线程池里面的任务还没有执行完,而队列也是满的,线程池处理不了这么多任务了,抛出异常java.util.concurrent.RejectedExecutionException。

综上:先创建核心线程,够数后往队列里面塞,塞满继续创建执行线程,再满后抛出拒绝执行的异常。我们在执行的时候希望往池子里面一直扔,盛不下了也别抛异常,怎么办?

可以通过判断池子的大小,如果已经满了,则阻塞添加:

private static void LinkedBlockingQueue(){
        ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
                .setNameFormat("demo-pool-%d").build();
        int queueCapacity = 3, corePoolSize=2, maximumPoolSize=3;
        LinkedBlockingQueue linkedBlockingQueue = new LinkedBlockingQueue(queueCapacity);
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(corePoolSize,maximumPoolSize,10, TimeUnit.SECONDS,linkedBlockingQueue,namedThreadFactory);
        for(int i=1;i<21;i++){
            Thread thread = namedThreadFactory.newThread(new Task(i));
            System.out.println("即将添加数据:"+i);
            threadPoolExecutor.execute(thread);
            System.out.println("i:"+i+", queueSize:"+linkedBlockingQueue.size()
                    +", poolSize:"+threadPoolExecutor.getPoolSize()
                    +", coreSize:"+threadPoolExecutor.getCorePoolSize()
                    +", maxSize:"+threadPoolExecutor.getMaximumPoolSize());
            while((threadPoolExecutor.getPoolSize()+linkedBlockingQueue.size())==(queueCapacity + maximumPoolSize)){
                System.out.println("线程池已满,休眠等待 i:"+i+", queueSize:"+linkedBlockingQueue.size()
                        +", poolSize:"+threadPoolExecutor.getPoolSize()
                        +", coreSize:"+threadPoolExecutor.getCorePoolSize()
                        +", maxSize:"+threadPoolExecutor.getMaximumPoolSize());
                try {
                    Thread.sleep(1000L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

        threadPoolExecutor.shutdown();
        while (true){
            if(threadPoolExecutor.isTerminated()){
                System.out.println("run over");
                break;
            }
        }
    }

在for循环里面进行了判断:

(threadPoolExecutor.getPoolSize()+linkedBlockingQueue.size())==(queueCapacity + maximumPoolSize),表示当前队列里面的线程数加上线程池里面当前线程数等于当前线程池可处理的最大线程的时候,进行Thread.sleep(1000L)等待,知道线程池有空闲资源的时候继续执行添加操作。

避免异常,可以使用阻塞队列ArrayBlockingQueue 

private static void ArrayBlockingQueue(){
        ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
                .setNameFormat("demo-pool-%d").build();
        int queueCapacity = 3, corePoolSize=2, maximumPoolSize=3;
        ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue(queueCapacity);
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(corePoolSize,maximumPoolSize,10, TimeUnit.SECONDS,arrayBlockingQueue,namedThreadFactory);
        for(int i=1;i<21;i++){
            int finalI = i;
            if((threadPoolExecutor.getPoolSize()+arrayBlockingQueue.size())>=(queueCapacity+maximumPoolSize)){
                try {
                    Thread thread = namedThreadFactory.newThread(new Task(finalI));
                    arrayBlockingQueue.put(thread);
                    System.out.println("队列中添加线程 i:"+i+", queueSize:"+arrayBlockingQueue.size()
                            +", poolSize:"+threadPoolExecutor.getPoolSize()
                            +", coreSize:"+threadPoolExecutor.getCorePoolSize()
                            +", maxSize:"+threadPoolExecutor.getMaximumPoolSize());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }else {
                Thread thread = namedThreadFactory.newThread(new Task(finalI));
                threadPoolExecutor.execute(thread);
                System.out.println("i:"+i+", queueSize:"+arrayBlockingQueue.size()
                        +", poolSize:"+threadPoolExecutor.getPoolSize()
                        +", coreSize:"+threadPoolExecutor.getCorePoolSize()
                        +", maxSize:"+threadPoolExecutor.getMaximumPoolSize());
            }
        }
        threadPoolExecutor.shutdown();
        while (true){
            if(threadPoolExecutor.isTerminated()){
                System.out.println("over");
                break;
            }
        }
        System.out.println( );
    }

使用ArrayBlockingQueue 阻塞机制,来实现同Thread.sleep()同样的效果。

线程池如何从队列里面取线程?

 

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: import java.util.concurrent.Executors; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit;public class ThreadPoolExecutorExample { public static void main(String[] args){ ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(2); for (int i = 1; i <= 5; i++) { Task task = new Task("Task " + i); System.out.println("Created : " + task.getName()); executor.execute(task); } executor.shutdown(); } }class Task implements Runnable { private String name; public Task(String name) { this.name = name; } public String getName() { return name; } public void run() { try { Long duration = (long) (Math.random() * 10); System.out.println("Executing : " + name); TimeUnit.SECONDS.sleep(duration); } catch (InterruptedException e) { e.printStackTrace(); } } }答:ThreadPoolExecutor示例的代码如下:import java.util.concurrent.Executors; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit;public class ThreadPoolExecutorExample { public static void main(String[] args){ ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(2); for (int i = 1; i <= 5; i++) { Task task = new Task("任务 " + i); System.out.println("已创建:" + task.getName()); executor.execute(task); } executor.shutdown(); } }class Task implements Runnable { private String name; public Task(String name) { this.name = name; } public String getName() { return name; } public void run() { try { Long duration = (long) (Math.random() * 10); System.out.println("正在执行:" + name); TimeUnit.SECONDS.sleep(duration); } catch (InterruptedException e) { e.printStackTrace(); } } } ### 回答2: ThreadPoolExecutor是Java中用来创建和管理线程池的类,可以提高线程的重用性和执行效率。下面是一个详细的代码示例说明如何使用ThreadPoolExecutor。 首先,我们需要导入需要的类: ```java import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; ``` 然后,我们可以使用ThreadPoolExecutor类来创建一个线程池,并指定线程池的属性: ```java int corePoolSize = 5; // 核心线程数 int maxPoolSize = 10; // 最大线程数 long keepAliveTime = 5000; // 线程空闲时间 TimeUnit unit = TimeUnit.MILLISECONDS; // 线程空闲时间的单位 ThreadPoolExecutor executor = new ThreadPoolExecutor(corePoolSize, maxPoolSize, keepAliveTime, unit, new LinkedBlockingQueue<Runnable>()); // 使用LinkedBlockingQueue作为线程队列 ``` 接下来,我们可以向线程池中提交任务: ```java executor.execute(new Runnable() { public void run() { // 执行任务代码 System.out.println("Hello World!"); } }); ``` 或者使用Java 8的Lambda表达式简化代码: ```java executor.execute(() -> { // 执行任务代码 System.out.println("Hello World!"); }); ``` 提交任务后,线程池会自动创建和管理线程,并执行任务。 最后,当使用完线程池后,为了释放资源,需要调用线程池的shutdown()方法来关闭线程池: ```java executor.shutdown(); // 关闭线程池 ``` 这就是ThreadPoolExecutor的一个简单示例。通过使用ThreadPoolExecutor,我们可以方便地管理线程池,提高线程的重用性和执行效率。 ### 回答3: ThreadPoolExecutor 是 Java 中用于并发编程的一个线程池实现类。它通过管理和复用线程来提高程序的执行效率。下面给出一个使用 ThreadPoolExecutor 的详细代码示例: ```java import java.util.concurrent.*; public class ThreadPoolExecutorExample { public static void main(String[] args) { // 创建一个线程池,核心线程数为2,最大线程数为4,任务队列容量为10 ThreadPoolExecutor executor = new ThreadPoolExecutor( 2, // 核心线程数 4, // 最大线程数 1, // 线程空闲时间 TimeUnit.SECONDS, // 时间单位 new ArrayBlockingQueue<>(10) // 任务队列 ); // 提交10个任务给线程池执行 for (int i = 1; i <= 10; i++) { final int taskId = i; executor.execute(() -> { System.out.println("Task " + taskId + " is running on thread " + Thread.currentThread().getName()); try { Thread.sleep(1000); // 模拟任务执行时间 } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Task " + taskId + " is completed"); }); } // 关闭线程池 executor.shutdown(); } } ``` 在上面的示例中,我们创建了一个 ThreadPoolExecutor 对象,设置了核心线程数为2,最大线程数为4,线程空闲时间为1秒,任务队列容量为10。 然后我们通过 execute 方法向线程池提交了10个任务,每个任务打印自己的任务编号并模拟一个任务运行时间为1秒的操作。 最后,我们调用 shutdown 方法关闭线程池。 注意,由于线程池的线程数有限,当任务数量超过核心线程数时,超出部分的任务将被放入任务队列中等待执行。当任务队列也满了后,线程池会按照设置的最大线程数创建新的线程来执行任务。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值