编程语言-Java系列Java工程师知识树

Java基础-并发编程-线程执行器executor

2021-02-20  本文已影响0人  HughJin

Java工程师知识树 / Java基础


线程实现方式

Thread、Runnable、Callable

//实现Runnable接口的类将被Thread执行,表示一个基本任务
public interface Runnable {    
    //run方法就是它所有内容,就是实际执行的任务    
    public abstract void run();
}
//Callable同样是任务,与Runnable接口的区别在于它接口泛型,同时它执行任务候带有返回值;
//Callable的使用通过外层封装成Future来使用
public interface Callable<V> {    
    //相对于run方法,call方法带有返回值    
    V call() throws Exception;
}

注意:启动Thread线程只能用start(JNI方法)来启动,start方法通知虚拟机,虚拟机通过调用器映射到底层操作系统,通过操作系统来创建线程来执行当前任务的run方法

Executor框架

Executor接口是线程池框架中最基础的部分,定义了一个用于执行Runnable的execute方法。从图中可以看出Exectuor下有一个重要的子接口ExecutorService,其中定义了线程池的具体行为:

ExecutorService中的重点属性:

private final AtomicInteger ctl = new AtomicInteger(ctlOf(RUNNING, 0));
private static final int COUNT_BITS = Integer.SIZE - 3;
private static final int CAPACITY   = (1 << COUNT_BITS) - 1;

ctl:对线程池的运行状态和线程池中有效线程的数量进行控制的一个字段,它包含两部分信息:线程池的运行状态(runState)和线程池内有效线程的数量(workerCount)。

这里可以看到,使用Integer类型来保存,高3位保存runState,低29位保存workerCount。COUNT_BITS 就是29,CAPACITY 就是1左移29位减1(29个1),这个常量表示workerCount的上限值,大约是5亿。

ctl相关方法:

//获取运行状态
private static int runStateOf(int c){ 
    return c & ~CAPACITY; 
}
//获取活动线程数
private static int workerCountOf(int c)  { 
    return c & CAPACITY; 
}
//获取运行状态和活动线程数的值
private static int ctlOf(int rs, int wc) { 
    return rs | wc; 
}

线程池的状态:

    RUNNING = -1 << COUNT_BITS; //高3位为111
    SHUTDOWN = 0 << COUNT_BITS; //高3位为000
    STOP = 1 << COUNT_BITS; //高3位为001
    TIDYING = 2 << COUNT_BITS; //高3位为010
    TERMINATED = 3 << COUNT_BITS; //高3位为011

线程池实例的状态

1、RUNNING

2、SHUTDOWN

3、STOP

4、TIDYING

5、TERMINATED

线程池使用

线程池使用

ThreadPoolExecutor构造方法

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.corePoolSize = corePoolSize;
        this.maximumPoolSize = maximumPoolSize;
        this.workQueue = workQueue;
        this.keepAliveTime = unit.toNanos(keepAliveTime);
        this.threadFactory = threadFactory;
        this.handler = handler;
}

任务的提交流程

执行图:

上一篇 下一篇

猜你喜欢

热点阅读