【JAVA中级篇】线程池

上一篇文章已经介绍了线程的基本概念以及线程相关的API,下面来看一下线程池

一、线程池框架

线程池的优点

  1. 重用线程池中的线程,避免因为线程的创建和销毁所带来的性能开销。 
  2. 能有效控制线程池的最大并发数,避免大量线程之间因互相抢夺系统资源而导致的阻塞现象。 
  3. 能够对线程进行简单的管理,并提供定时执行以及指向间隔循环执行等功能。

JAVA中相关类

线程池任务处理过程

线程池状态 

  1. RUNNING:运行状态,线程池创建好之后就会进入此状态,如果不手动调用关闭方法,那么线程池在整个程序运行期间都是此状态。
  2. SHUTDOWN:关闭状态,不再接受新任务提交,但是会将已保存在任务队列中的任务处理完。
  3. STOP:停止状态,不再接受新任务提交,并且会中断当前正在执行的任务、放弃任务队列中已有的任务。
  4. TIDYING:整理状态,所有的任务都执行完毕后(也包括任务队列中的任务执行完),当前线程池中的活动线程数降为 0 时的状态。到此状态之后,会调用线程池的 terminated() 方法。
  5. TERMINATED:销毁状态,当执行完线程池的 terminated() 方法之后就会变为此状态。

ThreadPoolExecutor类源码

构造方法的七个参数
                              int corePoolSize,//核心线程数量
                              int maximumPoolSize,//最大线程数量
                              long keepAliveTime,//非核心线程空闲时间
                              TimeUnit unit,//非核心线程空闲时间的单位
                              BlockingQueue<Runnable> workQueue,//等待队列
                              ThreadFactory threadFactory,//线程工厂
                              RejectedExecutionHandler handler//拒绝策略
重要属性
    private final BlockingQueue<Runnable> workQueue;//任务缓存队列,用来存放等待执行的任务

    private final ReentrantLock mainLock = new ReentrantLock();//线程池的主要状态锁,对线程池状态(比如线程池大小、runState等)的改变都要使用这个锁

    private final HashSet<Worker> workers = new HashSet<Worker>();//用来存放工作集

    private final Condition termination = mainLock.newCondition();//

    private int largestPoolSize;//用来记录线程池中曾经出现过的最大线程数

    private long completedTaskCount;//用来记录已经执行完毕的任务个数

    private volatile ThreadFactory threadFactory;//线程工厂,用来创建线程

    private volatile RejectedExecutionHandler handler;//任务拒绝策略

    private volatile long keepAliveTime;//线程存活时间 

    private volatile boolean allowCoreThreadTimeOut;//是否允许为核心线程设置存活时间

    private volatile int corePoolSize;//核心池的大小(即线程池中的线程数目大于这个参数时,提交的任务会被放进任务缓存队列)

    private volatile int maximumPoolSize;//线程池最大能容忍的线程数

    private static final RejectedExecutionHandler defaultHandler =
        new AbortPolicy();//默认的任务拒绝策略

    private static final RuntimePermission shutdownPerm =
        new RuntimePermission("modifyThread");
重要方法execute
    public void execute(Runnable command) {
        if (command == null)
            throw new NullPointerException();
        int c = ctl.get();
        if (workerCountOf(c) < corePoolSize) {
            if (addWorker(command, true))
                return;
            c = ctl.get();
        }
        if (isRunning(c) && workQueue.offer(command)) {
            int recheck = ctl.get();
            if (! isRunning(recheck) && remove(command))
                reject(command);
            else if (workerCountOf(recheck) == 0)
                addWorker(null, false);
        }
        else if (!addWorker(command, false))
            reject(command);
    }

线程池简单使用Demo

package org.cc.lipiao;

import java.time.LocalDateTime;
import java.util.concurrent.*;
import lombok.Getter;

public class ThreadPoolDemo {
    private static volatile boolean flag = true;
    public static void main(String[] args) throws InterruptedException {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2,4,4,
                TimeUnit.SECONDS,new ArrayBlockingQueue<>(6), Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy()
//                new ThreadPoolExecutor.CallerRunsPolicy()
//                new ThreadPoolExecutor.DiscardPolicy()
//                new ThreadPoolExecutor.DiscardOldestPolicy()

        );

        for (int i = 0; i < 15; i++) {
            MyThread thread = new MyThread("task" + i);
            try{
                threadPoolExecutor.execute(thread);
                Thread.sleep(1000);
            }
            catch (Exception e){
                System.out.println(String.format("任务%s提交后抛出异常,原因:%s",thread.getTaskName(),e.getMessage()));
                flag = false;
            }
            finally {
                System.out.println(String.format("核心线程数%d\n,最大线程数%d\n,达到过的最大线程数:%d\n,正在执行任务的线程数:%d\n" +
                                "当前池中的线程总数:%d\n已经完成任务数%d\n等待队列中的任务数%d\n",threadPoolExecutor.getCorePoolSize(),threadPoolExecutor.getMaximumPoolSize(),
                        threadPoolExecutor.getLargestPoolSize(),threadPoolExecutor.getActiveCount(),threadPoolExecutor.getPoolSize(),
                        threadPoolExecutor.getCompletedTaskCount(),threadPoolExecutor.getQueue().size()));
            }
        }
        System.out.println("==================="+ LocalDateTime.now()+"=========================");
        if(flag){
            flag = false;
        }
        for (int i = 0; i < 5; i++) {
            Thread.sleep(2000);
            System.out.println(String.format("核心线程数%d\n,最大线程数%d\n,达到过的最大线程数:%d\n,正在执行任务的线程数:%d\n" +
                            "当前池中的线程总数:%d\n已经完成任务数%d\n等待队列中的任务数%d\n",threadPoolExecutor.getCorePoolSize(),threadPoolExecutor.getMaximumPoolSize(),
                    threadPoolExecutor.getLargestPoolSize(),threadPoolExecutor.getActiveCount(),threadPoolExecutor.getPoolSize(),
                    threadPoolExecutor.getCompletedTaskCount(),threadPoolExecutor.getQueue().size()));
        }
        threadPoolExecutor.shutdown();
    }

    @Getter
    private static class MyThread extends Thread{
        private String taskName;
        public MyThread(String taskName) {
            this.taskName = taskName;
        }

        @Override
        public void run() {
            if("main".equals(Thread.currentThread().getName())){
                //当使用CallerRunsPolicy拒绝策略时,由主线程处理任务
                flag = false;
            }
            while (flag){

            }
            System.out.println(String.format("任务%s由线程%s执行完毕",this.taskName,Thread.currentThread()));
        }
    }
}

简单使用Demo2

package com.demo;
 
import java.util.ArrayList;
import java.util.Collection;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
 
public class ThreadPool {   
     
    private int threadCount = 10;
     
    private int threadpoolCount = 3;
     
    public static void main(String[] args) {
    	new ThreadPool().threadPoolControl();
    }
    
    public void threadPoolControl() {       
        ThreadObject[] et = new ThreadObject[threadCount];       
        ExecutorService service = Executors.newFixedThreadPool(threadpoolCount);
        Collection<ThreadObject> c = new ArrayList<ThreadObject>();
        for (int i = 0; i < threadCount; i++) {   
            et[i] = new ThreadObject();           
            c.add(et[i]);
        }
        try {
            service.invokeAll(c);
            service.shutdown();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    
    class ThreadObject implements Callable<Object>{       
        public Object call() throws Exception {
        	System.out.println("当前线程:"+Thread.currentThread().getName()+",线程对象:"+this);
            return null;
        }
    }
}

线程池的分类

newFixedThreadPool

newCachedThreadPool

newScheduledThreadPool

newWorkStealingPool

newSingleThreadExecutor

newSingleThreadScheduledExecutor

ThreadPoolExecutor的三种队列SynchronousQueue,LinkedBlockingQueue,ArrayBlockingQueue - 网络终结者 - 博客园 (cnblogs.com)

ThreadPoolExecutor线程池原理详解_哔哩哔哩_bilibili

  • 5
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java线程池是一种用于管理和复用线程的机制,它可以提高多线程应用程序的性能和效率。线程池中的线程可以被重复使用,避免了频繁创建和销毁线程的开销。 在Java中,线程池可以通过`ExecutorService`接口来创建和管理。线程池中的线程可以执行提交给它的任务,并且可以根据需要自动创建新的线程或销毁闲置的线程。 嵌套线程池是指在一个线程池中创建另一个线程池。这种情况通常发生在需要处理一些复杂的任务,其中每个任务本身也需要使用线程池来执行。 下面是一个示例代码,演示了如何在Java中嵌套使用线程池: ```java import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class NestedThreadPoolExample { public static void main(String[] args) { // 创建外层线程池 ExecutorService outerThreadPool = Executors.newFixedThreadPool(5); // 提交任务给外层线程池 outerThreadPool.execute(() -> { // 创建内层线程池 ExecutorService innerThreadPool = Executors.newFixedThreadPool(3); // 提交任务给内层线程池 innerThreadPool.execute(() -> { // 内层线程池执行的任务 System.out.println("Inner thread pool task executed"); }); // 关闭内层线程池 innerThreadPool.shutdown(); }); // 关闭外层线程池 outerThreadPool.shutdown(); } } ``` 在上面的示例中,我们首先创建了一个外层线程池`outerThreadPool`,它使用`Executors.newFixedThreadPool()`方法创建了一个固定大小的线程池。然后,我们向外层线程池提交了一个任务,该任务在执行时创建了一个内层线程池`innerThreadPool`,并向内层线程池提交了一个任务。最后,我们分别关闭了内层线程池和外层线程池

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值