java线程池源码分析

从线程池使用进行实现分析
一.自定义线程池
1.自定义线程池
2.构造完成之后状态
3.关键参数介绍
二.执行任务
1.execute一个任务
2.执行分析
三.线程池停止
1.shutDown分析
2.shutDownNow分析
四.线程池常见问题

一.自定义线程池

1.自定义线程池

//阻塞队列
LinkedBlockingQueue blockingQueue= new LinkedBlockingQueue(10);
//线程工厂
ThreadFactory threadFactory=new ThreadFactory() {
        @Override
        public Thread newThread(Runnable r) {
            Thread t = new Thread(r);
            t.setName("test");
            return t;
        }
    };
//拒绝策略
RejectedExecutionHandler rejectedExecutionHandler=new RejectedExecutionHandler() {
        @Override
        public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
            System.out.println("自定义拒绝策略");
        }
    };

//核心线程数
Integer corePoolSize =3;
//最大线程数
Integer maxPoolSize=10;
//空闲线程等待时间
Integer keepAliveTime=7;
//构造方法   
 ExecutorService threadPoolExecutor=new ThreadPoolExecutor(corePoolSize, maxPoolSize, keepAliveTime,
            TimeUnit.SECONDS, blockingQueue, threadFactory, rejectedExecutionHandler);

2.构造完成之后状态

java线程池源码分析_第1张图片
构造方法完成之后状态

3.几个关键参数

ctl:保存线程池存活状态,线程池内线程数

workqueque:自定义的任务队列

mainLock:线程池的锁

termination:mainLock.newCondition()阻塞/通知线程;

keepAliveTime:非核心线程的存活时间

corePoolSize:3 

maximumPoolSize:10

workers:线程集合 线程池内存活的线程数=0 

二.执行任务

1.execute一个任务


java线程池源码分析_第2张图片
execute之后状态

workers: 0 到 1

2.执行分析

1.首先检查存活线程数量
2.根据存活数量进行不同处理
3.开辟新的线程/添加到任务队列/开新线程至最大线程数/拒绝任务
4.如果成功的开了线程,调用线程的start()开始处理
5.worker用while循环不断获取任务.
6.直到当前任务和任务队列都为空,判断是否阻塞直至有新任务到来.

下面开始就源码进行分析

 public void execute(Runnable command) {
        if (command == null)
            throw new NullPointerException();
        /*
         * Proceed in 3 steps:
         *
         * 1. If fewer than corePoolSize threads are running, try to
         * start a new thread with the given command as its first
         * task.  The call to addWorker atomically checks runState and
         * workerCount, and so prevents false alarms that would add
         * threads when it shouldn't, by returning false.
         *
         * 2. If a task can be successfully queued, then we still need
         * to double-check whether we should have added a thread
         * (because existing ones died since last checking) or that
         * the pool shut down since entry into this method. So we
         * recheck state and if necessary roll back the enqueuing if
         * stopped, or start a new thread if there are none.
         *
         * 3. If we cannot queue task, then we try to add a new
         * thread.  If it fails, we know we are shut down or saturated
         * and so reject the task.
         */
         1.获取线程池状态
        int c = ctl.get();
        2.取workers数量 和如果小于核心线程数,生成一个新的worker
        if (workerCountOf(c) < corePoolSize) {
            if (addWorker(command, true))
                return;
            c = ctl.get();
        }
        3.workers.count到达了核心线程数,线程池是运行状态,把任务添加到任务队列
        if (isRunning(c) && workQueue.offer(command)) {
            int recheck = ctl.get();
            4.再次检查状态,如果不是运行状态,将任务移除出队列
            if (! isRunning(recheck) && remove(command))
                reject(command);
                5.如果此时线程池内没有线程了,再生成一个线程
            else if (workerCountOf(recheck) == 0)
                addWorker(null, false);
        }
        6.如果worker加不上去了,执行拒绝策略
        else if (!addWorker(command, false))
            reject(command);
    }
从上面可以看到每来一个任务都线程数量会判断使用策略
1.如果没有到核心线程,添加新线程执行任务
2.如果到核心线程,添加到任务队列
3.如果队列都满了,那就添加新线程,知道最大线程数.

分析addworker之前需要对worker这个内部类进行了解

java线程池源码分析_第3张图片
worker结构
可以看到,本质上就是对线程的包装.主要是继承了AQS,通过锁对线程进行保护.

下面开始看addWorks方法

private boolean addWorker(Runnable firstTask, boolean core) {
        retry:
        for (;;) {
        1.进来先获取线程池状态
            int c = ctl.get();
            int rs = runStateOf(c);
            // Check if queue empty only if necessary.
            2.校验线程池状态
            if (rs >= SHUTDOWN &&
                ! (rs == SHUTDOWN &&
                   firstTask == null &&
                   ! workQueue.isEmpty()))
                return false;
            for (;;) {
                int wc = workerCountOf(c);
                3.先看是否达到最大容量.再判断是否到达自定义规定容量
                if (wc >= CAPACITY ||
                    wc >= (core ? corePoolSize : maximumPoolSize))
                    return false;
                if
            4.这里采取的cas算法让worker计数器+1.如果cas失败,重复1234.
            (compareAndIncrementWorkerCount(c))
                    break retry;
                c = ctl.get();  // Re-read ctl
                if (runStateOf(c) != rs)
                    continue retry;
                // else CAS failed due to workerCount change; retry inner loop
            }
        }
        5.下面开始正式创建worker
        boolean workerStarted = false;
        boolean workerAdded = false;
        Worker w = null;
        try {
        6.获得线程池的锁
            final ReentrantLock mainLock = this.mainLock;
            7.初始化worker,构造方法里创建了线程
            w = new Worker(firstTask);
            final Thread t = w.thread;
            if (t != null) {
            8.因为works是hashSet不是线程安全的,所以这里需要加锁处理.里面添加worker,更新相关计数器
                mainLock.lock();
                try {
                    // Recheck while holding lock.
                    // Back out on ThreadFactory failure or if
                    // shut down before lock acquired.
                    int c = ctl.get();
                    int rs = runStateOf(c);
                    if (rs < SHUTDOWN ||
                        (rs == SHUTDOWN && firstTask == null)) {
                        if (t.isAlive()) // precheck that t is startable
                            throw new IllegalThreadStateException();
                        workers.add(w);
                        int s = workers.size();
                        if (s > largestPoolSize)
                            largestPoolSize = s;
                        workerAdded = true;
                    }
                } finally {
                    mainLock.unlock();
                }
                9.如果worker添加成功.直接启动线程
                if (workerAdded) {
                11.start方法实际调用worker runWorker()方法
                    t.start();
                    workerStarted = true;
                }
            }
        } finally {
        10.如果线程启动失败,worker数量减少,尝试中断失败线程.
            if (! workerStarted)
                addWorkerFailed(w);
        }
        return workerStarted;
    }

对runWorker进行分析

final void runWorker(Worker w) {
        Thread wt = Thread.currentThread();
        Runnable task = w.firstTask;
        w.firstTask = null;
        猜测这里unlock主要避免执行shutdownNow时并发问题
        w.unlock(); // allow interrupts
        boolean completedAbruptly = true;
        try {
        1.这里有两种方式获取任务 getTask会根据情况是否阻塞线程.
        getTask下面分析
            while (task != null || (task = getTask()) != null) {
        2.这里采用的独占锁,保证了任务开始执行后,只有在锁之前已经产生的终止状态才能使自我中断。
                w.lock();
                // If pool is stopping, ensure thread is interrupted;
                // if not, ensure thread is not interrupted.  This
                // requires a recheck in second case to deal with
                // shutdownNow race while clearing interrupt
                if ((runStateAtLeast(ctl.get(), STOP) ||
                     (Thread.interrupted() &&
                      runStateAtLeast(ctl.get(), STOP))) &&
                    !wt.isInterrupted())
                    wt.interrupt();
                try {
                    beforeExecute(wt, task);
                    Throwable thrown = null;
                    try {
                    3.这里终于开始执行任务了!!!
                        task.run();
                    } catch (RuntimeException x) {
                        thrown = x; throw x;
                    } catch (Error x) {
                        thrown = x; throw x;
                    } catch (Throwable x) {
                        thrown = x; throw new Error(x);
                    } finally {
                        afterExecute(task, thrown);
                    }
                } finally {
                    task = null;
                    w.completedTasks++;
                    w.unlock();
                }
            }
            completedAbruptly = false;
        } finally {
        4.最后根据状态判断线程是否可以正常退出.
            processWorkerExit(w, completedAbruptly);
        }
    }
线程不断获取任务执行,并且用独占锁保护线程执行过程,这样就保证了shutdown时后,正在执行任务的县城不会被中断,但是shutdownNow不受此影响.可以直接粗暴的中断线程.

getTask()分析

private Runnable getTask() {
        boolean timedOut = false; // Did the last poll() time out?

        retry:
        for (;;) {
            int c = ctl.get();
            int rs = runStateOf(c);

            // Check if queue empty only if necessary.
            1.如果线程池调用了shutDown,线程池状态就是shutdown.如果调用shutdown线程池状态就是stop,这2个策略主要是后续任务处理方式不一样.
            如果是shutdown状态,队列必须不为空,那么可以继续从任务队列获取任务进行处理
            如果是stop状态,队列不为空也不进行处理.(shutDownNow时任务队列已经把任务返回了)
            if (rs >= SHUTDOWN && (rs >= STOP || workQueue.isEmpty())) {
                decrementWorkerCount();
                return null;
            }

            boolean timed;      // Are workers subject to culling?

            for (;;) {
                int wc = workerCountOf(c);
                timed = allowCoreThreadTimeOut || wc > corePoolSize;

                if (wc <= maximumPoolSize && ! (timedOut && timed))
                    break;
                if (compareAndDecrementWorkerCount(c))
                    return null;
                c = ctl.get();  // Re-read ctl
                if (runStateOf(c) != rs)
                    continue retry;
                // else CAS failed due to workerCount change; retry inner loop
            }

            try {
            2.判断是阻塞一定时间还是一直阻塞直到取到任务.
                Runnable r = timed ?
                    workQueue.poll(keepAliveTime, TimeUnit.NANOSECONDS) :
                    workQueue.take();
                if (r != null)
                    return r;
                timedOut = true;
            } catch (InterruptedException retry) {
                timedOut = false;
            }
        }
    }
这里通过多重判断,来决定线程是阻塞在这里等任务,还是存活keepAliveTime.
1.如果线程数是最大线程数,并且任务队列为空,那么阻塞空闲线程还是存活keepAliveTime时间,期间没有任务到来,让其向后执行,自然中断.
2.如果线程数是核心线程数,根据allowCoreThreadTimeOut处理。true情况下阻塞keepAliveTime时间,期间没有任务到来,让其向后执行,自然中断.false情况下,一直阻塞,直到有任务加入队列.

三.线程池停止

1.shutDown分析

 public void shutdown() {
        final ReentrantLock mainLock = this.mainLock;
        mainLock.lock();
        try {
            1.这里应该是做一些权限检查
            checkShutdownAccess();
            2.利用CAS将变线程池转变成shutDown状态
            advanceRunState(SHUTDOWN);
            3.中断空闲线程
            interruptIdleWorkers();
            onShutdown(); // hook for ScheduledThreadPoolExecutor
        } finally {
            mainLock.unlock();
        }
        4.最后尝试将线程池状态设置为TIDYING状态
        tryTerminate();
    }

interruptIdleWorkers

 private void interruptIdleWorkers(boolean onlyOne) {
        final ReentrantLock mainLock = this.mainLock;
        mainLock.lock();
        try {
            for (Worker w : workers) {
                Thread t = w.thread;
                1.首先线程不是中断状态,尝试获取线程的锁.这里tryLock的实现就决定了
                if (!t.isInterrupted() && w.tryLock()) {
                    try {
                    2.如果能拿到锁终端线程
                        t.interrupt();
                    } catch (SecurityException ignore) {
                    } finally {
                        w.unlock();
                    }
                }
                if (onlyOne)
                    break;
            }
        } finally {
            mainLock.unlock();
        }
    }
这里是先获取线程锁,获取不到不终止,所以不能中断任务中的线程,并且会把队列中的任务处理完

tryLock

tryLock里面调用了tryAcquire
protected boolean tryAcquire(int unused) {
            1.这里采用了独占锁的实现方式 0到1才能获取锁.所以保证了正在执行任务的线程不会被shutdown中断
            if (compareAndSetState(0, 1)) {
                setExclusiveOwnerThread(Thread.currentThread());
                return true;
            }
            return false;
        }
这是自定义了aqs获取锁的方式

2.shutDownNow分析

public List shutdownNow() {
        List tasks;
        final ReentrantLock mainLock = this.mainLock;
        mainLock.lock();
        try {
            checkShutdownAccess();
            advanceRunState(STOP);
            1.第1个区别尝试中断所有线程
            interruptWorkers();
            1.第2个区别,把队列中的任务返回给调用者处理
            tasks = drainQueue();
        } finally {
            mainLock.unlock();
        }
        tryTerminate();
        return tasks;
    }

interruptWorkers();

    private void interruptWorkers() {
        final ReentrantLock mainLock = this.mainLock;
        mainLock.lock();
        try {
            for (Worker w : workers)
            1.这里就粗暴多了,直接终止,可能导致有些线程不能正常终止,处于异常状态.
                w.interruptIfStarted();
        } finally {
            mainLock.unlock();
        }
    }
    
    ====================================================
    void interruptIfStarted() {
            Thread t;
            if (getState() >= 0 && (t = thread) != null && !t.isInterrupted()) {
                try {
                    t.interrupt();
                } catch (SecurityException ignore) {
                }
            }
        }

直接粗暴的终止线程,不管线程上没上锁.

线程池常见问题
1.线程池对线程包装的作用

2.关键参数(allowCoreThreadTimeOut,keepAliveTime,corePoolSize,maximumPoolSize,ctl)

3.关键技术(CAS,AbstractQueuedSynchronizer,ReentrantLock,LinkedB lockingQueue)

4.线程池线程数动态变化过程

5.shutDown和shutDownNow执行上有什么区别

6.excute和submit区别

7.核心线程是什么情况下减少到0

你可能感兴趣的:(java线程池源码分析)