1、今天在项目中使用了线程池,在这里简单的记录一下,一面以后回忘记。话不多说直接上代码。
package com.proven.thread;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
*
* @ClassName: ThreadPoolDemo
* @Description: TODO(用来做线程池的demo)
* @author proven
* @date 2019年4月3日
*
*/
public class ThreadPoolDemo {
public static void main(String[] args) {
ThreadPoolDemo demo = new ThreadPoolDemo();
demo.doProcess();
}
private void doProcess(){
ExecutorService exec = null;
try{
exec = new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors(),
Runtime.getRuntime().availableProcessors() * 2, 60, TimeUnit.SECONDS,new LinkedBlockingQueue<>());
for(int i=0;i<10;i++){
TestPoolThreadRunnable thread = new TestPoolThreadRunnable(i);
exec.execute(thread);
}
exec.shutdown();
try {
while(exec.awaitTermination(10, TimeUnit.SECONDS));
} catch (Exception e) {
e.printStackTrace();
}
}catch(Exception e){
e.printStackTrace();
}
System.out.println("thread finished");
}
class TestPoolThreadRunnable implements Runnable{
private Integer count;
public TestPoolThreadRunnable(Integer count){
this.count = count;
}
/**
* <p>Title: run</p>
* <p>Description: </p>
* @see java.lang.Runnable#run()
*/
@Override
public void run() {
System.out.println(count++);
}
}
}
这是我在自己电脑上写的demo,在这里简单介绍下代码。
首先创建一个线程池的代码是这样的:
exec = new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors(),
Runtime.getRuntime().availableProcessors() * 2, 60, TimeUnit.SECONDS,new LinkedBlockingQueue<>());
从jdk 源码中可以知道这几个参数的意思,简单介绍下几个参数:
/**
* Creates a new {@code ThreadPoolExecutor} with the given initial
* parameters and default thread factory and rejected execution handler.
* It may be more convenient to use one of the {@link Executors} factory
* methods instead of this general purpose constructor.
*
* @param corePoolSize the number of threads to keep in the pool, even
* if they are idle, unless {@code allowCoreThreadTimeOut} is set
* @param maximumPoolSize the maximum number of threads to allow in the
* pool
* @param keepAliveTime when the number of threads is greater than
* the core, this is the maximum time that excess idle threads
* will wait for new tasks before terminating.
* @param unit the time unit for the {@code keepAliveTime} argument
* @param workQueue the queue to use for holding tasks before they are
* executed. This queue will hold only the {@code Runnable}
* tasks submitted by the {@code execute} method.
* @throws IllegalArgumentException if one of the following holds:<br>
* {@code corePoolSize < 0}<br>
* {@code keepAliveTime < 0}<br>
* {@code maximumPoolSize <= 0}<br>
* {@code maximumPoolSize < corePoolSize}
* @throws NullPointerException if {@code workQueue} is null
*/
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue) {
this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
Executors.defaultThreadFactory(), defaultHandler);
}
corePoolSize:这个参数表示创建线程池的时候,创建的线程数量。
maximumPoolSize:这个是最大的线程数量。
keepAliveTime:当线程数量大于最大线程数量的时候,这是多余的空闲线程在终止前等待新任务的最长时间。
unit:时间的单位
workQueue:执行线程之前的队列
还有一段代码要单独拿出来讲一下
try {
while(exec.awaitTermination(10, TimeUnit.SECONDS));
} catch (Exception e) {
e.printStackTrace();
}
加上这段代码的主要作用就是所有线程执行完成才会跳出当前方法,如果不加的话当还在执行线程的时候,就会执行其他方法。