线程池(一)----综述&ThreadPoolExecutor

线程池简介:

在Java中,我们一般通过集成Thread类和实现Runnnable接口,调用线程的start()方法实现线程的启动。但如果并发的数量很多,而且每个线程都是执行很短的时间便结束了,那样频繁的创建线程和销毁进程会大大的降低系统运行的效率。线程池正是为了解决多线程效率低的问题而产生的,他使得线程可以被复用,就是线程执行结束后不被销毁,而是可以继续执行其他任务。(这里可以用tomcat做例子进行思考),一言以蔽之,就是提高系统效率和吞吐量。 

在《阿里巴巴java开发手册》中就指出了线程资源必须通过线程池提供,不允许在应用中自行显示的创建线程,这样一方面是线程的创建更加规范,可以合理控制开辟线程的数量;另一方面线程的细节管理交给线程池处理,优化了资源的开销。而线程池不允许使用Executors去创建,而要通过ThreadPoolExecutor方式,这一方面是由于jdk中Executor框架虽然提供了如newFixedThreadPool()、newSingleThreadExecutor()、newCachedThreadPool()等创建线程池的方法,但都有其局限性,不够灵活;另外由于前面几种方法内部也是通过ThreadPoolExecutor方式实现,使用ThreadPoolExecutor有助于明确线程池的运行规则,创建符合自己的业务场景需要的线程池,避免资源耗尽的风险。后来的ThreadPoolTaskExecutor则是对ThreadPoolExecutor做了一些封装。

建议在Spring项目中使用ThreadPoolTaskExecutor,对ThreadPoolExecutor做了很多封装,使用简单。

java.util.concurrent.ThreadPoolExecutor

是ExecutorSerivce接口的具体实现。ThreadPoolExecutor使用线程池中的一个线程来执行给定的任务(Runnable或者Runnable)。是用来处理异步任务的一个接口,可以将其理解成为一个线程池和一个任务队列,提交到 ExecutorService 对象的任务会被放入任务队或者直接被线程池中的线程执行。

继承关系:

public interface Executor  
 
    public interface ExecutorService extends Executor
 
         AbstractExecutorService implements ExecutorService
 
                 public class ThreadPoolExecutor extends AbstractExecutorService     
一、构造函数与参数:

public ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue<Runnable> workQueue,
                              ThreadFactory threadFactory,
                              RejectedExecutionHandler handler) {
        if (corePoolSize < 0 ||
            maximumPoolSize <= 0 ||
            maximumPoolSize < corePoolSize ||
            keepAliveTime < 0)
            throw new IllegalArgumentException();
        if (workQueue == null || threadFactory == null || handler == null)
            throw new NullPointerException();
        this.acc = System.getSecurityManager() == null ?
                null :
                AccessController.getContext();
        this.corePoolSize = corePoolSize;
        this.maximumPoolSize = maximumPoolSize;
        this.workQueue = workQueue;
        this.keepAliveTime = unit.toNanos(keepAliveTime);
        this.threadFactory = threadFactory;
        this.handler = handler;
    }

构造函数的参数含义如下:

corePoolSize:线程组保留的最小线程数,如果线程组中的线程数少于此数目,则创建。指定了线程池中的线程数量,它的数量决定了添加的任务是开辟新的线程去执行,还是放到workQueue任务队列中去;

maximumPoolSize:指定了线程池中的最大线程数量,这个参数会根据你使用的workQueue任务队列的类型,决定线程池会开辟的最大线程数量;

keepAliveTime:当线程池中空闲线程数量超过corePoolSize时,多余的线程会在多长时间内被销毁;

unit:keepAliveTime的单位,为枚举

workQueue:任务队列,被添加到线程池中,但尚未被执行的任务;它一般分为直接提交队列、有界任务队列、无界任务队列、优先任务队列几种;

threadFactory:线程工厂,用于创建线程,一般用默认即可;

handler:拒绝策略;当任务太多来不及处理时,如何拒绝任务;

---------------------------------------ThreadPoolExecutor池子的处理流程如下:  

1)当运行的线程数小于corePoolSize的时候 ,创建新的线程即Worker执行提交的任务

2)、如果线程数大于等于corePoolSize的时候,将任务提交到workQueue队列中 ,如果成功添加 ,runWorker就会执行调用了,当然这里会重新的核查此时的线程数,看下是否有线程减少,如果减少,则创建新的线程来使线程数维持在corePoolSize的数目

3)、如果队列满了后,则创建新的线程来执行,当然这里有一种极端情况,当线程数等于maximumPoolSize时,并且workQueue也满了后,就会抛出错 误 org.springframework.core.task.TaskRejectedException,另外MaxPoolSize的设定如果比系统支持的线程数还要大时,会抛出java.lang.OutOfMemoryError: unable to create new native thread 异常。

二、重要参数详解:

1、workQueue任务队列:

(1)直接提交队列:设置为SynchronousQueue队列,SynchronousQueue是一个特殊的BlockingQueue,它没有容量,每执行一个插入操作就会阻塞,需要再执行一个删除操作才会被唤醒,反之每一个删除操作也都要等待对应的插入操作。希望提交的任务尽快分配线程执行建议使用此

例:

import java.util.concurrent.Executors;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
 
public class Demo {
 
    private static ThreadPoolExecutor pool;
    public static void main( String[] args )
    {
        //maximumPoolSize设置为2 ,拒绝策略为AbortPolic策略,直接抛出异常
        pool = new ThreadPoolExecutor(1,
                2,
                1000,
                TimeUnit.MILLISECONDS,
                new SynchronousQueue<Runnable>(),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());
        for(int i=0;i<3;i++) {
            pool.execute(new DemoTask());
        }   
    }
}

package exceldemo;
 
public class DemoTask implements Runnable{
 
    public DemoTask() {
 
    }
 
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName());
    }
 
}
输出结果:

Exception in thread "main" java.util.concurrent.RejectedExecutionException: Task exceldemo.DemoTask@63961c42 rejected from java.util.concurrent.ThreadPoolExecutor@65b54208[Running, pool size = 2, active threads = 2, queued tasks = 0, completed tasks = 0]
    at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2047)
    at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:823)
    at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1369)
    at exceldemo.Demo.main(Demo.java:15)
pool-1-thread-1
pool-1-thread-2
可以看到,当任务队列为SynchronousQueue,创建的线程数大于maximumPoolSize时,直接执行了拒绝策略抛出异常。

即,使用SynchronousQueue队列,提交的任务不会被保存,总是会马上提交执行。如果用于执行任务的线程数量小于maximumPoolSize,则尝试创建新的进程,如果达到maximumPoolSize设置的最大值,则根据你设置的handler执行拒绝策略。因此这种方式你提交的任务不会被缓存起来,而是会被马上执行,在这种情况下,你需要对你程序的并发量有个准确的评估,才能设置合适的maximumPoolSize数量,否则很容易就会执行拒绝策略;

(2) 有界的任务队列:有界的任务队列可以使用ArrayBlockingQueue实现:

pool = new ThreadPoolExecutor(1, 2, 1000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),Executors.defaultThreadFactory(),new ThreadPoolExecutor.AbortPolicy());
 
for(int i=0;i<3;i++) {
            pool.execute(new DemoTask());
        }
输出结果:

pool-1-thread-1
pool-1-thread-1
pool-1-thread-1
如果任务数过多:

pool = new ThreadPoolExecutor(1,
                2,
                1000,
                TimeUnit.MILLISECONDS,
                new ArrayBlockingQueue<Runnable>(10),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());
        for(int i=0;i<13;i++) {
            pool.execute(new DemoTask());
        }   
 
 
 
也会报错:
Exception in thread "main" java.util.concurrent.RejectedExecutionException: Task exceldemo.DemoTask@5674cd4d rejected from java.util.concurrent.ThreadPoolExecutor@63961c42[Running, pool size = 2, active threads = 2, queued tasks = 10, completed tasks = 0]
    at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2047)
    at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:823)
    at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1369)
    at exceldemo.Demo.main(Demo.java:20)
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
pool-1-thread-1
pool-1-thread-2

使用ArrayBlockingQueue有界任务队列,若有新的任务需要执行时,线程池会创建新的线程,直到创建的线程数量达到corePoolSize时,则会将新的任务加入到等待队列中。若等待队列已满,即超过ArrayBlockingQueue初始化的容量,则继续创建线程,直到线程数量达到maximumPoolSize设置的最大线程数量,若大于等于maximumPoolSize,则执行拒绝策略。在这种情况下,线程数量的上限与有界任务队列的状态有直接关系,如果有界队列初始容量较大或者没有达到超负荷的状态,线程数将一直维持在corePoolSize以下,反之当任务队列已满时,则会以maximumPoolSize为最大线程数上限。

(3) 无界的任务队列:可以使用LinkedBlockingQueue实现,对于需要保证所有提交的任务都要被执行的情况建议使用此

//maximumPoolSize设置为2 ,拒绝策略为AbortPolic策略,直接抛出异常
        pool = new ThreadPoolExecutor(1,
                2,
                1000,
                TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>(),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());
        for(int i=0;i<13;i++) {
            pool.execute(new DemoTask());
        }
输出结果:

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
pool-1-thread-1
pool-1-thread-1
pool-1-thread-1
任务数过多也不会报错。 

 使用无界任务队列,线程池的任务队列可以无限制的添加新的任务,而线程池创建的最大线程数量就是你corePoolSize设置的数量,也就是说在这种情况下maximumPoolSize这个参数是无效的,哪怕你的任务队列中缓存了很多未执行的任务,当线程池的线程数达到corePoolSize后,就不会再增加了;若后续有新的任务加入,则直接进入队列等待,当使用这种任务队列模式时,一定要注意你任务提交与处理之间的协调与控制,不然会出现队列中的任务由于无法及时处理导致一直增长,直到最后资源耗尽的问题。

(4)优先任务队列:优先任务队列通过PriorityBlockingQueue实现,

package exceldemo;
public class DemoTask implements Runnable,Comparable<DemoTask>{
 
    private int priority;
 
    public int getPriority() {
        return priority;
    }
 
    public void setPriority(int priority) {
        this.priority = priority;
    }
 
    public DemoTask() {
 
    }
 
    public DemoTask(int priority) {
        this.priority = priority;
    }
 
    //当前对象和其他对象做比较,当前优先级大就返回-1,优先级小就返回1,值越小优先级越高
    @Override
    public int compareTo(DemoTask o) {
        return  this.priority>o.priority?-1:1;
    }
 
    @Override
    public void run() {
        try {
            //让线程阻塞,使后续任务进入缓存队列
            Thread.sleep(1000);
            System.out.println("priority:"+this.priority+",ThreadName:"+Thread.currentThread().getName());
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
 
    }
}

package exceldemo;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.*;
public class Demo {
 
private static ThreadPoolExecutor pool;
 
    public static void main( String[] args )
    {
        //maximumPoolSize设置为2 ,拒绝策略为AbortPolic策略,直接抛出异常
        //优先任务队列
                pool = new ThreadPoolExecutor(1, 2, 1000, TimeUnit.MILLISECONDS, new PriorityBlockingQueue<Runnable>(),Executors.defaultThreadFactory(),new ThreadPoolExecutor.AbortPolicy());
 
        for(int i=0;i<10;i++) {
            pool.execute(new DemoTask(i));
        }
    }
}
 
 

执行结果:

priority:0,ThreadName:pool-1-thread-1
priority:9,ThreadName:pool-1-thread-1
priority:8,ThreadName:pool-1-thread-1
priority:7,ThreadName:pool-1-thread-1
priority:6,ThreadName:pool-1-thread-1
priority:5,ThreadName:pool-1-thread-1
priority:4,ThreadName:pool-1-thread-1
priority:3,ThreadName:pool-1-thread-1
priority:2,ThreadName:pool-1-thread-1
priority:1,ThreadName:pool-1-thread-1
 可以看到除了第一个任务直接创建线程执行外,其他的任务都被放入了优先任务队列,按优先级进行了重新排列执行,且线程池的线程数一直为corePoolSize,也就是只有一个。PriorityBlockingQueue它其实是一个特殊的无界队列,它其中无论添加了多少个任务,线程池创建的线程数也不会超过corePoolSize的数量,只不过其他队列一般是按照先进先出的规则处理任务,而PriorityBlockingQueue队列可以自定义规则根据任务的优先级顺序先后执行。

2、拒绝策略

一般我们创建线程池时,为防止资源被耗尽,任务队列都会选择创建有界任务队列,这种模式下如果出现任务队列已满且线程池创建的线程数达到你设置的最大线程数时,就需要你指定合理的拒绝策略,来处理线程池"超载"的情况。ThreadPoolExecutor自带的拒绝策略及举例如下:

package exceldemo;
 
public class DemoTask implements Runnable{
 
    public DemoTask() {
 
    }
 
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName());
    }
 
}
(1)AbortPolicy策略:该策略会直接抛出异常,阻止系统正常工作;

package exceldemo;
 
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
 
import java.util.concurrent.*;
 
public class Demo {
 
  private static ThreadPoolExecutor pool;
 
    public static void main( String[] args )
    {
        pool = new ThreadPoolExecutor(1,
                2,
                1000,
                TimeUnit.MILLISECONDS,
                new ArrayBlockingQueue<Runnable>(5),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());
                for(int i=0;i<10;i++) {
                   pool.execute(new DemoTask());
               }
    }
}
 
 
 
 
 
 
结果:
Exception in thread "main" java.util.concurrent.RejectedExecutionException: Task exceldemo.DemoTask@5674cd4d rejected from java.util.concurrent.ThreadPoolExecutor@63961c42[Running, pool size = 2, active threads = 2, queued tasks = 5, completed tasks = 0]
pool-1-thread-2
pool-1-thread-1
    at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2047)
pool-1-thread-2
pool-1-thread-1
    at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:823)
pool-1-thread-2
    at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1369)
pool-1-thread-2
    at exceldemo.Demo.main(Demo.java:19)
pool-1-thread-1

(2)CallerRunsPolicy策略:如果线程池的线程数量达到上限,该策略会把任务队列中的任务放在调用者线程当中运行;

package exceldemo;
 
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
 
import java.util.concurrent.*;
 
public class Demo {
 
   private static ThreadPoolExecutor pool;
 
    public static void main( String[] args )
    {
        pool = new ThreadPoolExecutor(1,
                2,
                1000,
                TimeUnit.MILLISECONDS,
                new ArrayBlockingQueue<Runnable>(5),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.CallerRunsPolicy());
                for(int i=0;i<10;i++) {
                   pool.execute(new DemoTask());
               }
    }
}
 
 
 
 
结果:
main
main
main
pool-1-thread-1
pool-1-thread-1
pool-1-thread-2
pool-1-thread-1
pool-1-thread-1
pool-1-thread-2
pool-1-thread-1

(3)DiscardOledestPolicy策略:该策略会丢弃任务队列中最老的一个任务,也就是当前任务队列中最先被添加进去的,马上要被执行的那个任务,并尝试再次提交;

package exceldemo;
 
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
 
import java.util.concurrent.*;
 
public class Demo {
 
  private static ThreadPoolExecutor pool;
 
    public static void main( String[] args )
    {
        pool = new ThreadPoolExecutor(1,
                2,
                1000,
                TimeUnit.MILLISECONDS,
                new ArrayBlockingQueue<Runnable>(5),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.DiscardOldestPolicy());
                for(int i=0;i<10;i++) {
                   pool.execute(new DemoTask());
               }
    }
}
 
 
结果:
pool-1-thread-1
pool-1-thread-2
pool-1-thread-2
pool-1-thread-2
pool-1-thread-1
pool-1-thread-1
pool-1-thread-2
 
 

(4)DiscardPolicy策略:该策略会默默丢弃无法处理的任务,不予任何处理。当然使用此策略,业务场景中需允许任务的丢失;

package exceldemo;
 
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
 
import java.util.concurrent.*;
 
public class Demo {
 
  private static ThreadPoolExecutor pool;
 
    public static void main( String[] args )
    {
        pool = new ThreadPoolExecutor(1,
                2,
                1000,
                TimeUnit.MILLISECONDS,
                new ArrayBlockingQueue<Runnable>(5),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.DiscardPolicy());
                for(int i=0;i<10;i++) {
                   pool.execute(new DemoTask());
               }
    }
}
 
 
结果:
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-2

注:以上内置的策略均实现了RejectedExecutionHandler接口,当然也可以自己扩展RejectedExecutionHandler接口,自定义拒绝策略,如:

package exceldemo;
 
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
 
import java.util.concurrent.*;
 
public class Demo {
 
  private static ThreadPoolExecutor pool;
 
    public static void main( String[] args )
    {
        pool = new ThreadPoolExecutor(1, 2, 1000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(5),
                Executors.defaultThreadFactory(), new RejectedExecutionHandler() {
            public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                System.out.println(r.toString()+"执行了拒绝策略");
 
            }
        });
                for(int i=0;i<10;i++) {
                   pool.execute(new DemoTask());
               }
    }
}
 
 
结果:
exceldemo.DemoTask@5674cd4d执行了拒绝策略
exceldemo.DemoTask@63961c42执行了拒绝策略
exceldemo.DemoTask@65b54208执行了拒绝策略
pool-1-thread-2
pool-1-thread-1
pool-1-thread-2
pool-1-thread-1
pool-1-thread-2
pool-1-thread-1
pool-1-thread-2

3、 ThreadFactory自定义线程创建: 线程池中线程就是通过ThreadPoolExecutor中的ThreadFactory,线程工厂创建的。自定义ThreadFactory,可以按需要对线程池中创建的线程进行一些特殊的设置,如命名、优先级等,如:

package exceldemo;
 
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
 
import java.util.concurrent.*;
 
public class Demo {
 
   private static ThreadPoolExecutor pool;
 
    public static void main( String[] args )
    {
        //自定义线程工厂
                pool = new ThreadPoolExecutor(1, 2, 1000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(5),
                new ThreadFactory() {
                    public Thread newThread(Runnable r) {
                        System.out.println("线程"+r.hashCode()+"创建");
                        //线程命名
                        Thread th = new Thread(r,"threadPool"+r.hashCode());
                        return th;
                    }
                }, new ThreadPoolExecutor.CallerRunsPolicy());
 
        for(int i=0;i<10;i++) {
            pool.execute(new DemoTask());
        }
    }
}
 
 
结果:
线程2061475679创建
线程140435067创建
main
main
main
threadPool2061475679
threadPool2061475679
threadPool2061475679
threadPool2061475679
threadPool2061475679
threadPool2061475679
threadPool140435067

4、ThreadPoolExecutor扩展 :ThreadPoolExecutor扩展主要是围绕beforeExecute()、afterExecute()和terminated()三个接口实现的,

beforeExecute:线程池中任务运行前执行

afterExecute:线程池中任务运行完毕后执行

terminated:线程池退出后执行

举例:

package exceldemo;
 
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
 
import java.util.concurrent.*;
 
public class Demo {
 
 private static ThreadPoolExecutor pool;
 
    public static void main( String[] args )
    {
        //自定义线程工厂
                //实现自定义接口
                pool = new ThreadPoolExecutor(2, 4, 1000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(5),
                new ThreadFactory() {
                    public Thread newThread(Runnable r) {
                        System.out.println("线程"+r.hashCode()+"创建");
                        //线程命名
                        Thread th = new Thread(r,"threadPool"+r.hashCode());
                        return th;
                    }
                },
                new ThreadPoolExecutor.CallerRunsPolicy()) {
 
            @Override
            protected void beforeExecute(Thread t,Runnable r) {
                System.out.println("准备执行:"+ ((DemoTask)r).getTaskName());
            }
 
            @Override
            protected void afterExecute(Runnable r,Throwable t) {
                System.out.println("执行完毕:"+((DemoTask)r).getTaskName());
            }
 
            @Override
            protected void terminated() {
                System.out.println("线程池退出");
            }
        };
 
        for(int i=0;i<10;i++) {
            pool.execute(new DemoTask("Task"+i));
        }
        pool.shutdown();
 
    }
}

package exceldemo;
 
public class DemoTask implements Runnable{
 
    private String taskName;
    public String getTaskName() {
        return taskName;
    }
    public void setTaskName(String taskName) {
        this.taskName = taskName;
    }
    public DemoTask(String name) {
        this.setTaskName(name);
    }
 
    @Override
    public void run() {
        //输出执行线程的名称
        System.out.println("TaskName"+this.getTaskName()+"---ThreadName:"+Thread.currentThread().getName());
    }
}

执行结果:
线程2061475679创建
线程140435067创建
线程1450495309创建
准备执行:Task0
线程1670782018创建
TaskNameTask0---ThreadName:threadPool2061475679
TaskNameTask9---ThreadName:main
执行完毕:Task0
准备执行:Task2
准备执行:Task1
TaskNameTask2---ThreadName:threadPool2061475679
执行完毕:Task2
准备执行:Task3
TaskNameTask3---ThreadName:threadPool2061475679
准备执行:Task7
执行完毕:Task3
TaskNameTask7---ThreadName:threadPool1450495309
执行完毕:Task7
准备执行:Task5
准备执行:Task4
TaskNameTask5---ThreadName:threadPool1450495309
TaskNameTask4---ThreadName:threadPool2061475679
TaskNameTask1---ThreadName:threadPool140435067
执行完毕:Task5
执行完毕:Task1
准备执行:Task6
TaskNameTask6---ThreadName:threadPool1450495309
执行完毕:Task4
执行完毕:Task6
准备执行:Task8
TaskNameTask8---ThreadName:threadPool1670782018
执行完毕:Task8
线程池退出

5、线程池数量:

线程吃线程数量的设置没有一个明确的指标,根据实际情况,只要不是设置的偏大和偏小都问题不大,结合下面这个公式即可 

/**
             * Nthreads=CPU数量
             * Ucpu=目标CPU的使用率,0<=Ucpu<=1
             * W/C=任务等待时间与任务计算时间的比率
             */
            Nthreads = Ncpu*Ucpu*(1+W/C)
6、提交任务:

(1)无返回值的任务使用public void execute(Runnable command) 方法提交,子线程可能在主线程结束之后结束;

(2)有返回值的任务使用public <T> Future<T> submit(Callable) 方法提交,因为提交任务后有个取数据的过程,在从Future取数据的过程中,Callable自带的阻塞机制,这个机制保证主线程一定在子线程结束之后结束。反之如果没有取数据,子线程可能会在主线程结束之后才结束。

详见下下一篇博客。

三、应用规范:

1、整个项目中只初始化一次ThreadPoolExecutor;

2、线程与主线程(调用处)是异步执行的,需要注意参数传递的正确性,

举例如下:如用户表有id、user_name、age,现在根据id集合查询用户列表,当id集合过大,使用in查询效率低,如果采用循环,单线程又比较耗时,这时可以使用多线程去处理。

package exceldemo.service.impl;
 
import exceldemo.dto.User;
import exceldemo.service.UserService;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.List;
 
@Service("userService")
public class UserServiceImpl implements UserService {
    @Override
    public List<User> getByIds(List<Integer> ids) {
 
        List<User> users = new ArrayList<>();
        for(Integer id : ids){
            User user = new User();
            user.setAge(id);
            user.setUserName("用户"+id);
            //耗时操作
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            users.add(user);
        }
        return users;
    }
}

先看普通的单线程写法: 

package exceldemo.rest;
 
import exceldemo.dto.User;
import exceldemo.service.UserService;
import exceldemo.thread.MyThreadPoolExecutor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.*;
 
@RestController
@RequestMapping("/user")
public class UserController {
 
    @Autowired
    private UserService userService;
 
 
 
    //获取所有用户
    @RequestMapping("/getAll")
    public List<User> getAll( ){
        List<Integer> ids = new ArrayList<>();
        for(int i = 0;i<=500;i++){
            ids.add(i);
        }
        long startTime = new Date().getTime();
        List<User> users = new ArrayList<>();
        List<Integer> queryIds = new ArrayList<>();
        for(Integer id : ids){
            queryIds.add(id);
            if(queryIds.size() == 100){
                users.addAll(userService.getByIds(queryIds));
                queryIds.clear();
            }
        }
        if(queryIds.size() > 0){
            users.addAll(userService.getByIds(queryIds));
        }
        long endTime = new Date().getTime();
        System.out.println("耗时"+(endTime-startTime));
        return users;
    }
}

请求后后端打印:

耗时5055
多线程:

package exceldemo.thread;
 
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
 
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
 
public class MyThreadPoolExecutor {
 
    private static ThreadPoolExecutor threadPoolExecutor = new  ThreadPoolExecutor(10,
            21,
            1000,
            TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<Runnable>(),
            Executors.defaultThreadFactory(),
            new ThreadPoolExecutor.AbortPolicy());;
 
    public static ThreadPoolExecutor getThreadPoolInstance(){
          System.out.println(threadPoolExecutor);
          return threadPoolExecutor;
    }
}

package exceldemo.task;
 
import exceldemo.dto.User;
import exceldemo.service.UserService;
 
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
 
public class UserTask implements Callable<List<User>> {
 
    private List<Integer> userIds;
 
    private UserService userService;
 
    public UserTask(List<Integer> userIds,UserService userService) {
        this.userIds = userIds;
        this.userService = userService;
    }
 
    @Override
    public List<User> call() throws Exception {
 
        List<User> users = userService.getByIds(userIds);
        return users;
    }
}

package exceldemo.rest;
 
import exceldemo.dto.User;
import exceldemo.service.UserService;
import exceldemo.task.UserTask;
import exceldemo.thread.MyThreadPoolExecutor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.*;
 
@RestController
@RequestMapping("/user")
public class UserController {
 
    @Autowired
    private UserService userService;
 
 
 
    //获取所有用户
    @RequestMapping("/getAll")
    public List<User> getAll( ){
        List<Integer> ids = new ArrayList<>();
        for(int i = 0;i<=500;i++){
            ids.add(i);
        }
        long startTime = new Date().getTime();
        //List<User> users = userService.getByIds(ids);
        //多线程处理开始*******************************************
        List<Future> futures = new ArrayList<>();
        for (int i = 0; i < ids.size(); i += 100) {
            int startIndex = i;
            int endIndex = startIndex + 100 > ids.size() ? ids.size() : startIndex + 100;
            UserTask task = new UserTask(ids.subList(startIndex, endIndex),userService);
            Future<List<User>> future = MyThreadPoolExecutor.getThreadPoolInstance().submit(task);
            futures.add(future);
        }
        //取数据
        List<User> users = new ArrayList<>();
        try{
            for (Future futureIter : futures) {
                List<User> personnelDetailDOS = (List<User>) futureIter.get();
                users.addAll(personnelDetailDOS);
            }
        }catch (Exception e){
 
        }
 
        //多线程处理结束*******************************************
 
        long endTime = new Date().getTime();
        System.out.println("耗时"+(endTime-startTime));
        return users;
    }
}

结果:

耗时1023
获取的结果也和单线程是一样的,完全正确。

下面采用一种错误的批量写法来看下:

ThreadPoolExecutor excutor = MyThreadPoolExecutor.getThreadPoolInstance();  
for(Integer id : ids){
            queryIds.add(id);
            if(queryIds.size() == 100){
                UserTask task = new UserTask(queryIds,userService);
                Future<List<User>> future = excutor.submit(task);
                futures.add(future);
                queryIds.clear();
            }
        }
        if(queryIds.size() > 0){
            UserTask task = new UserTask(queryIds,userService);
            Future<List<User>> future = excutor.submit(task);
            futures.add(future);
        }

请求的结果不正确且随机:

[
  {
    "userName": "用户100",
    "age": 100
  },
  {
    "userName": "用户300",
    "age": 300
  },
  {
    "userName": "用户300",
    "age": 300
  },
  {
    "userName": "用户400",
    "age": 400
  },
  {
    "userName": "用户500",
    "age": 500
  },
  {
    "userName": "用户500",
    "age": 500
  }
]

但是在单线程中controller批量调用dubbo服务这样写是没有问题的,这是因为应用多线程时,submit(execute)方法实际调用的为Callable(Runnable)的call(run),这个call和run与主线程不是同步在执行。如在本例中传递的是一个对象,而对象是引用传递,那可能有很多种情况:

1)如果call在queryIds.clear之后,在且只在下一个childIds.add(100)之后被调度执行,这时call方法里面拿到的对象List<Integer>就是一个100了;

2)如果call在queryIds.clear之后,且在下一个childIds.add(id)之前被调度执行,这时call方法里面拿到的对象List<Integer>就是空了

......

而第一种写法给每个UserTask传递的参数就是一个List,且这个List在它所在的UserTask生命周期中没有被改变,所以结果是正确的。由此可见,在异步操作时要注意参数的一致性。

当然,第二种种批量写法这样改造也是正确的:

ThreadPoolExecutor excutor = MyThreadPoolExecutor.getThreadPoolInstance();  
for(Integer id : ids){
            queryIds.add(id);
            if(queryIds.size() == 100){
               List<Integer> copyIds = new ArrayList<>();
                for(Integer copyId : queryIds){
                    copyIds.add(copyId);
                }
                UserTask task = new UserTask(copyIds,userService);
                Future<List<User>> future = excutor.submit(task);
                futures.add(future);
                queryIds.clear();
            }
        }
        if(queryIds.size() > 0){
            UserTask task = new UserTask(queryIds,userService);
            Future<List<User>> future = excutor.submit(task);
            futures.add(future);
        }

 execute提交保证线程安全:

List<User> users = userMapper.selectAll();
List<List<User>> userPageList= Lists.partition(users,100);       
for(int page = 0;page < userPageList.size();page++){
                int finalPage = page;
                threadPoolTaskExecutor.execute(()->{
                    List<User> statPersons = userPageList.get(finalPage);
                   //逻辑
                });

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿彬在上路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值