并发编程(3)——ThreadPoolExecutor

ThreadPoolExecutor

1. ctl(control state)

线程池控制状态,包含两个概念字段:workerCount(线程有效数量)和runState(表示是否在运行、关闭等状态)

workerCount限制到2^29 - 1 (5亿左右)

runstate有如下几个状态:

RUNNING: 接收新任务,并处理队列中的任务

SHUTDOWN: 不接收新任务,但处理队列中的任务

STOP: 不接收新任务,也不处理队列中任务,并且会中断运行中的任务

TIDYING: 所有任务都被终止,workerCount是0,过渡到TIDYING状态的线程会执行terminated()钩子方法

TERMINATED:terminated()方法执行完毕

几种常量:
RUNNING=-1<<29=-536870912,
SHUTDOWN=0 << 29=0,
STOP = 1 << 29=536870912,
TIDYING = 2 << 29=1073741824,
TERMINATED = 3 << 29 = 1610612736.

ctl初始值=RUNNING | 0 = -1 << 29=-536870912.

2. 构造器:

public ThreadPoolExecutor(    int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue<Runnable> workQueue,
                              ThreadFactory threadFactory,
                              RejectedExecutionHandler handler) 

corePoolSize: 线程池中保留的线程数量

maximumPoolSize: 线程池允许的最大线程数量

keepAliveTime:当线程池中数量大于corePoolSize,这是多余的空闲线程等待新任务的最大时间,超过则terminate

uinit: keepAliveTime的时间单位,通常是TimeUnit里的

workQueue: 队列

threadFactory: 主要用来给线程起名字,默认前缀"pool-" + poolNumber.getAndIncrement() + "-thread-";

handler: 拒绝策略。分四种:

DiscardOldestPolicy,丢弃最老的未处理请求,然后尝试执行

AbortPolicy, 直接丢弃(默认

CallerRunsPolicy,在调用线程里的execute方法里执行拒绝的任务

DiscardPolicy: 悄悄的丢弃拒绝的任务。

3. execute方法

	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.
         */
        int c = ctl.get(); 
        if (workerCountOf(c) < corePoolSize) { // 判断workerCount是否达到了corePoolSize
            if (addWorker(command, true))
                return;
            c = ctl.get();
        }
        if (isRunning(c) && workQueue.offer(command)) {
            int recheck = ctl.get();
            if (! isRunning(recheck) && remove(command))
                reject(command);
            else if (workerCountOf(recheck) == 0)
                addWorker(null, false);
        }
        else if (!addWorker(command, false))
            reject(command);
    }

看一下addWorker实现:

private boolean addWorker(Runnable firstTask, boolean core) {
        retry:
        for (;;) {// 1. 自旋过程主要进行一些校验,最主要的是增加wc
            int c = ctl.get();
            int rs = runStateOf(c);

            // Check if queue empty only if necessary.
            /** 这一部分参考各个状态的处理策略,rs>=shutdown的,不再接收新任务,分为三种情况
            * 1. rs是stop,tidying,terminated状态
            * 2. rs是shutdown,但firstTask != null
            * 3. rs是shutdown,workQueue队列为空
            * 符合这三种,则return false
            */
            if (rs >= SHUTDOWN &&
                ! (rs == SHUTDOWN &&
                   firstTask == null &&
                   ! workQueue.isEmpty()))
                return false;

            for (;;) {
                int wc = workerCountOf(c);
                // 判断线程数量是否超过最大容量或是否超过核心线程数量或者是最大线程数量
                if (wc >= CAPACITY ||
                    wc >= (core ? corePoolSize : maximumPoolSize))
                    return false;
                if (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
            }
        }

		// 下面这段启动worker
        boolean workerStarted = false;
        boolean workerAdded = false;
        Worker w = null;
        try {
	        // 注意:private final class Worker extends AbstractQueuedSynchronizer implements Runnable
            w = new Worker(firstTask);
            final Thread t = w.thread;
            if (t != null) {
                final ReentrantLock mainLock = this.mainLock;
                mainLock.lock();
                try {
                    // Recheck while holding lock.
                    // Back out on ThreadFactory failure or if
                    // shut down before lock acquired.
                    int rs = runStateOf(ctl.get());

                    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();
                }
                if (workerAdded) {
                    t.start();
                    workerStarted = true;
                }
            }
        } finally {
            if (! workerStarted)
                addWorkerFailed(w);
        }
        return workerStarted;
    }

t.start调用Worker的run方法,接着调用runWorker方法:

4. shutdown vs shutdownNow

shutdown:
主要两步:

  1. advanceRunState(SHUTDOWN);设置状态为SHUTDOWN
  2. interruptIdleWorkers();中断所有未在执行任务的线程
    这里,第二步判断如下:
				if (!t.isInterrupted() && w.tryLock()) {
                    try {
                        t.interrupt();
                    } catch (SecurityException ignore) {
                    } finally {
                        w.unlock();
                    }
                }

w.tryLock(),正如javadoc所说,因为Worker创建之后会运行,运行的时候runWorker(this)方法会获取锁,只有不在运行的任务才可以获取到锁。

 * Interrupts threads that might be waiting for tasks (as
 * indicated by not being locked) so they can check for
 * termination or configuration changes

shutdownNow()方法

  1. advanceRunState(STOP);设置状态为STOP
  2. interruptWorkers(); 中断所有线程
  3. tasks = drainQueue(); 返回等待执行任务的列表

5. Worker

类定义:
private final class Worker extends AbstractQueuedSynchronizer implements Runnable
成员变量: Thread thread——worker运行所在的线程
Runnable firstTask——初始运行任务,有可能是null.
其中的lock(),tryLock(),unlock(),tryAcquire(),tryRelease()等方法是AQS中的常用方法,这里暂且略过。
看一下Worker的run方法:
public void run() { runWorker(this); }

	final void runWorker(Worker w) {
        Thread wt = Thread.currentThread();
        Runnable task = w.firstTask;
        w.firstTask = null;
        w.unlock(); // allow interrupts
        boolean completedAbruptly = true;
        try {
        	// 运行完worker的初始任务firstTask之后,从workQueue中获取任务,直到任务为空
            while (task != null || (task = getTask()) != null) {
                w.lock();
                /**
                * 中断的一些判断,1. 线程池STOP状态并且当前线程未中断需要中断 
				* 2. Thread.interrupted()[In  other words, if this method were to be called twice in succession, the second call would return false]换句话讲,如果在shutdownNow方法中线程被中断,则interrupted状态应该为true。額,这里也没搞懂为什么要调用Thread.interrupted()来清除interrupted状态。
				*/
                // 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 {
                	// 和after一样是钩子方法hook
                    beforeExecute(wt, task);
                    Throwable thrown = null;
                    try {
                        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 {
            processWorkerExit(w, completedAbruptly);
        }
    }

6. 其他

  1. allowCoreThreadTimeOut, 默认false,核心线程即使空闲下来也会保证存活,如果是true的话,核心线程使用keepAliveTime超时时间。
  2. mainLock, 保持workers稳定,文件中获取锁都是通过mainLock.lock()来实现的。
  3. workers: 包含线程池中所有worker线程的Set。只有持有mainLock的锁,才可以访问。
  4. termination: 支持awaitTermination()的wait 条件,是Condition的实现AQS中的ConditionObject。
  5. runnable转Callable: Executors.callable(runnable, result)这里用了适配器RunnableAdapter
  6. ThreadPoolExecutor的submit参数无论是Runnable还是Callable,都是先转成RunnableFuture,再调用execute(Runnable runnable)方法。

Executors

常用的几种方法:

固定线程数的线程池

public static ExecutorService newFixedThreadPool(int nThreads) {    
return new ThreadPoolExecutor(nThreads, nThreads,0L, TimeUnit.MILLISECONDS,
                            new LinkedBlockingQueue<Runnable>());
}
public static ExecutorService newSingleThreadExecutor() {    
return new FinalizableDelegatedExecutorService(
  new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS,                                new LinkedBlockingQueue<Runnable>()));
}
public static ExecutorService newCachedThreadPool() {    
	return new ThreadPoolExecutor(0, Integer.MAX_VALUE,  60L, TimeUnit.SECONDS,                                  		new SynchronousQueue<Runnable>());
}

public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
  return new ScheduledThreadPoolExecutor(corePoolSize);
}

BlockingQueue

add(E e): 没有违反容量限制的话,插入成功返回true,如果没有位置插入的话,抛出IlleagalStateException。
offer(E e):没有违反容量限制的话,插入成功返回true,如果没有位置插入的话,返回false。当使用capacity-restricted队列时,优于add方法。
put (E e): 有空间的时候再进行插入。

take: 获取并删除队列头结点,阻塞直到有节点可用。
poll: Retrieves and removes the head of this queue, waiting up to the

 * specified wait time if necessary for an element to become available.

remove: 删除队列中的某个元素。

常见的一些实现:

  1. ArrayBlockingQueue
  2. SynchronousQueue
  3. DelayQueue
  4. PriorityBlockingQueue
当你准备好了,机会来临的时候,你才能抓住
原文地址:https://www.cnblogs.com/studentytj/p/11291056.html