1. 线程
1.1 线程的几种状态:
NEW:初始状态、线程被构建,但是还没有 start()RUNNABLE:运行状态,RUNING 和 READY 都是运行状态,start() 之后变成 READY 线程获取 CPU 的 timeslice 之后就处于 RUNNINGBLOCK:阻塞状态,线程阻塞于锁WAITING:等待状态,需要等待其它线程做出一些动作TIME_WAITING:超时等待,到达指定的时间自行返回TERMINATE:线程终止,已经执行完毕了
1.2 sleep 和 wait 的区别
sleep 没有释放锁,而 wait 释放了锁wait 主要和 notify 配合使用来进行线程间的通信,sleep 主要是用于暂停线程
1.3 线程创建的几种方式
继承 Thread,重写 run 方法实现 Runnable 方法,使用 Thread 的构造函数使用 Callable 和 FutureTask 创建线程
Callable
<String> callable
= new Thread3();
FutureTask
<String> futureTask
= new FutureTask<>(callable
);
Thread thread3
= new Thread(futureTask
);
使用线程池
Executor executor
= Executors
.newFixedThreadPool(5);
executor
.execute(new Runnable() {
@Override
public void run() {
System
.out
.println(Thread
.currentThread()+"创建线程的第四种方法");
}
});
2. Executor
组成
Runnable / Callable:任务,其接口的实现类都可以被 ThreadPoolExecutor 或 ScheduledThreadPoolExecutor 执行Executor:任务的执行Future:异步计算的结果,调用 submit 时返回的对象
流程
主线程创建实现 Runnable 或者 Callable 接口的任务对象把创建完成的实现接口的对象直接交给 ExecutorService 执行,execute(),submit()如果 submit,会返回一个 Future 的实现类对象最后主线程执行返回的 Future 对象的 get 方法来等待任务执行完成,主线程也可以执行 cancel 方法来取消任务的执行
3. ThreadPoolExecutor
阿里巴巴推荐使用 ThreadPoolExecutor 构建线程池
3.1 主要参数
corePoolSize:核心线程数,最小同时运行线程数量maximumPoolSize:最大线程数,队列达到容量时,可同时运行的线程数量变成最大线程数workQueue:工作队列,当前运行线程到达核心线程数的时候,任务进入工作队列keepAliveTime:当线程数大于核心线程数时,多余的空闲线程存活的最长时间(没有新任务提交的时候)unit:keepAliveTime 参数的时间单位threadFactory:线程工厂,用来创建线程,一般默认即可handler:拒绝策略,当提交的任务过多而不能及时处理时,我们可以定制策略来处理任务
3.2 拒绝策略
线程数量达到最大线程数,队列也满了的时候
AbortPolicy:默认的拒绝策略,直接抛出 RejectedExecutionExceptionCallerRunsPolicy:由调用线程(提交任务的线程)处理该任务,如果执行程序已关闭,则会丢弃该任务。因此这种策略会降低对于新任务提交速度,影响程序的整体性能。如果您的应用程序可以承受此延迟并且你要求任何一个任务请求都要被执行的话,你可以选择这个策略。DiscardPolicy:不处理新任务,直接丢弃掉DiscardOldestPolicy:丢弃最早的未处理的任务请求
3.3 Demo
public class Demo {
private static final int CORE_POOL_SIZE
= 5;
private static final int MAX_POOL_SIZE
= 10;
private static final int QUEUE_CAPACITY
= 100;
private static final Long KEEP_ALIVE_TIME
= 1L
;
public static void main(String
[] args
) {
ThreadPoolExecutor executor
= new ThreadPoolExecutor(
CORE_POOL_SIZE
,
MAX_POOL_SIZE
,
KEEP_ALIVE_TIME
,
TimeUnit
.SECONDS
,
new ArrayBlockingQueue<>(QUEUE_CAPACITY
),
new ThreadPoolExecutor.CallerRunsPolicy());
List
<Future
<String>> futureList
= new ArrayList<>();
Callable
<String> callable
= new MyCallable();
for (int i
= 0; i
< 10; i
++) {
Future
<String> future
= executor
.submit(callable
);
futureList
.add(future
);
}
for (Future
<String> fut
: futureList
) {
try {
System
.out
.println(new Date() + "::" + fut
.get());
} catch (InterruptedException | ExecutionException e
) {
e
.printStackTrace();
}
}
executor
.shutdown();
}
}
Wed Jul 01 21:51:35 CST 2020::pool-1-thread-1
Wed Jul 01 21:51:36 CST 2020::pool-1-thread-2
Wed Jul 01 21:51:36 CST 2020::pool-1-thread-3
Wed Jul 01 21:51:36 CST 2020::pool-1-thread-4
Wed Jul 01 21:51:36 CST 2020::pool-1-thread-5
Wed Jul 01 21:51:36 CST 2020::pool-1-thread-3
Wed Jul 01 21:51:37 CST 2020::pool-1-thread-1
Wed Jul 01 21:51:37 CST 2020::pool-1-thread-2
Wed Jul 01 21:51:37 CST 2020::pool-1-thread-5
Wed Jul 01 21:51:37 CST 2020::pool-1-thread-4
4. 常见的线程池
4.1 FixedThreadPool
public static ExecutorService
newFixedThreadPool(int nThreads
, ThreadFactory threadFactory
) {
return new ThreadPoolExecutor(nThreads
, nThreads
,
0L
, TimeUnit
.MILLISECONDS
,
new LinkedBlockingQueue<Runnable>(),
threadFactory
);
}
核心线程数和最大线程数被设置成了同一个值使用无界队列作为工作队列 LinkedBlockingQueue,队列不会被装满所以不会拒绝任务,可能会有 OOM
4.2 SingleThreadExecutor
public static ExecutorService
newSingleThreadExecutor(ThreadFactory threadFactory
) {
return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1, 1,
0L
, TimeUnit
.MILLISECONDS
,
new LinkedBlockingQueue<Runnable>(),
threadFactory
));
}
返回只有一个线程的线程池同样使用的也是无界队列
4.3 CachedThreadPool
public static ExecutorService
newCachedThreadPool(ThreadFactory threadFactory
) {
return new ThreadPoolExecutor(0, Integer
.MAX_VALUE
,
60L
, TimeUnit
.SECONDS
,
new SynchronousQueue<Runnable>(),
threadFactory
);
}
可能会创建大量的线程,极端情况下会 OOM