Java线程池 / Executor / Callable / Future

为什么需要线程池?

每次都要new一个thread,开销大,性能差;不能统一管理;功能少(没有定时执行、中断等)。

使用线程池的好处是,可重用,可管理。

Executor

4种线程池

// 可缓存线程池,如果缓存中没有可用的,则移出60秒未使用过的线程

ExecutorService service= Executors.new CachedThreadPool();

// 大小固定的线程池

ExecutorService service= Executors.new FixedThreadPool(5);

// 单线程,是线程量=1的FixedThreadPool,多任务下相当于排队。

ExecutorService service= Executors.new SingleThreadPool();

// 支持定时和周期,一般情况下可替代timer

ScheduledExecutorService exec = Executors.new ScheduledThreadPool(int corePoolSize)

Demo

ExecutorService pool= Executors.new CachedThredPool();

pool.ececute(new Runable());

// 关闭,不再接受新的任务请求

pool.shutdown();

// 立即关闭,停止接受task,

pool.showdownNow();

Future

.submit(Runnable) 返回一个Future,主要目的是检查任务执行的状况(是否完成,有无异常)。

interface Future

V get() throws …;

V get(long timeout,TimeUnit unit) throws ..;

void cancel(boolean mayInterrupt); // 取消任务,如果已经开始,mayInterrupt=true时被中断

boolean isCancelled();

boolean isDown();

Future task = pool.submit(new Runnable());

task.isDone(); //

task.get(); // 阻塞当前线程,如果task在执行过程中有异常,则会在这里重新抛出

Callable

Runnable没有返回值,Callable

submit一个runnable,不能知道运行结果,可以submit一个callable。

// 创建callable

class MyCallable implements Callable

@Override

public Integer call()

return 1;

Future

task.get(); // 阻塞,显示结果

FutureTask

同时包装了Future和Task。

Callable

FutureTask

new Thread(task).start();

Integer r = t.get();

CompletionService

completionService = new ExecutorCompletionService

for(){

completionService.submit(new Callable());

// 取完成的任务,如果没有,就阻塞等待

completionService.take().get()

Original: https://www.cnblogs.com/caca/p/java_executors.html
Author: cacard
Title: Java线程池 / Executor / Callable / Future

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/538511/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球