voidshutdownAndAwaitTermination(ExecutorService pool){ pool.shutdown(); // Disable new tasks from being submitted try { // Wait a while for existing tasks to terminate if (!pool.awaitTermination(60, TimeUnit.SECONDS)) { pool.shutdownNow(); // Cancel currently executing tasks // Wait a while for tasks to respond to being cancelled if (!pool.awaitTermination(60, TimeUnit.SECONDS)) System.err.println("Pool did not terminate"); } } catch (InterruptedException ie) { // (Re-)Cancel if current thread also interrupted pool.shutdownNow(); // Preserve interrupt status Thread.currentThread().interrupt(); } }
/** * 线程池的主控制状态 ctl ,用 atomic integer 来表示两个概念的字段 * workerCount, 指示当前有效的线程数 * runState, 指示当前的运行状态 running, shutting down etc * 为了把两个字段打包成一个int, 限制workerCount to * (2^29)-1 (about 500 million) threads rather than (2^31)-1 (2 * billion) otherwise representable. * * * The runState provides the main lifecyle control, taking on values: * * RUNNING: Accept new tasks and process queued tasks * SHUTDOWN: Don't accept new tasks, but process queued tasks * STOP: Don't accept new tasks, don't process queued tasks, * and interrupt in-progress tasks * TIDYING: All tasks have terminated, workerCount is zero, * the thread transitioning to state TIDYING * will run the terminated() hook method * TERMINATED: terminated() has completed * * 运行状态的次序很重要,可以用来进行比较。runState 随着时间单调递增的, but need not hit each state. The transitions are: * * RUNNING -> SHUTDOWN * On invocation of shutdown(), perhaps implicitly in finalize() * (RUNNING or SHUTDOWN) -> STOP * On invocation of shutdownNow() * SHUTDOWN -> TIDYING * When both queue and pool are empty * STOP -> TIDYING * When pool is empty * TIDYING -> TERMINATED * When the terminated() hook method has completed * * Threads waiting in awaitTermination() will return when the * state reaches TERMINATED. * * 为什么从SHUTDOWN 转到 TIDYING 需要检测,因为 queue有可能从non-emtpy 转变 empty , * but we can only terminate if, after seeing that it is empty, we see * that workerCount is 0 (which sometimes entails a recheck -- see * below). */ privatefinal AtomicInteger ctl = new AtomicInteger(ctlOf(RUNNING, 0)); privatestaticfinalint COUNT_BITS = Integer.SIZE - 3; privatestaticfinalint CAPACITY = (1 << COUNT_BITS) - 1;
// Packing and unpacking ctl privatestaticintrunStateOf(int c){ return c & ~CAPACITY; } privatestaticintworkerCountOf(int c){ return c & CAPACITY; } privatestaticintctlOf(int rs, int wc){ return rs | wc; } privatestaticbooleanisRunning(int c){ return c < SHUTDOWN; }
/** * Attempt to CAS-increment the workerCount field of ctl. */ privatebooleancompareAndIncrementWorkerCount(int expect){ return ctl.compareAndSet(expect, expect + 1); }
/** * Attempt to CAS-decrement the workerCount field of ctl. */ privatebooleancompareAndDecrementWorkerCount(int expect){ return ctl.compareAndSet(expect, expect - 1); }
/** * Decrements the workerCount field of ctl. This is called only on * abrupt termination of a thread (see processWorkerExit). Other * decrements are performed within getTask. */ privatevoiddecrementWorkerCount(){ do {} while (! compareAndDecrementWorkerCount(ctl.get())); }
/** * Class Worker mainly maintains interrupt control state for * threads running tasks, along with other minor bookkeeping. * This class opportunistically extends AbstractQueuedSynchronizer * to simplify acquiring and releasing a lock surrounding each * task execution. This protects against interrupts that are * intended to wake up a worker thread waiting for a task from * instead interrupting a task being run. We implement a simple * non-reentrant mutual exclusion lock rather than use * ReentrantLock because we do not want worker tasks to be able to * reacquire the lock when they invoke pool control methods like * setCorePoolSize. Additionally, to suppress interrupts until * the thread actually starts running tasks, we initialize lock * state to a negative value, and clear it upon start (in * runWorker). */ privatefinalclassWorker extendsAbstractQueuedSynchronizer implementsRunnable {
/** Thread this worker is running in. Null if factory fails. */ final Thread thread; /** Initial task to run. Possibly null. */ Runnable firstTask; /** Per-thread task counter */ volatilelong completedTasks;
/** * Creates with given first task and thread from ThreadFactory. * @param firstTask the first task (null if none) */ Worker(Runnable firstTask) { // 初始Worker状态为-1 setState(-1); // inhibit interrupts until runWorker this.firstTask = firstTask; // 创建一个新的Thread线程对象 this.thread = getThreadFactory().newThread(this); }
/** Delegates main run loop to outer runWorker */ publicvoidrun(){ // 这里调用外部的runWorker!!! runWorker(this); }
// Lock methods // // The value 0 represents the unlocked state. // The value 1 represents the locked state.
/** * Transitions to TERMINATED state if either (SHUTDOWN and pool * and queue empty) or (STOP and pool empty). If otherwise * eligible to terminate but workerCount is nonzero, interrupts an * idle worker to ensure that shutdown signals propagate. This * method must be called following any action that might make * termination possible -- reducing worker count or removing tasks * from the queue during shutdown. The method is non-private to * allow access from ScheduledThreadPoolExecutor. */ // 在每个 Worker 退时,会调用 tryTerminate 方法,所以这个方法会被调用多次 finalvoidtryTerminate(){ for (;;) { int c = ctl.get(); if (isRunning(c) || runStateAtLeast(c, TIDYING) || (runStateOf(c) == SHUTDOWN && ! workQueue.isEmpty())) // 处理以下状态直接返回 // 正在运行, // 已经>TIDYING 清理中 // SHUTDOWN ,但 workQueue不为空 return; // 活动线程数不为0里,中断一个空闲Worker并return if (workerCountOf(c) != 0) { // Eligible to terminate interruptIdleWorkers(ONLY_ONE); return; }
// 当活动工作线程数为0时 final ReentrantLock mainLock = this.mainLock; mainLock.lock(); try { // 设置为 TIDYING 状态 if (ctl.compareAndSet(c, ctlOf(TIDYING, 0))) { try { terminated(); } finally { // 设置为 TERMINATED 终止状态 ctl.set(ctlOf(TERMINATED, 0)); // 发送 termination.signalAll 信号, 在 awaitTermination 方法中等待该信号 termination.signalAll(); } return; } } finally { mainLock.unlock(); } // else retry on failed CAS } } //******************************** interruptWorkers *************************** /** * Interrupts all threads, even if active. Ignores SecurityExceptions * (in which case some threads may remain uninterrupted). */ // 强制中断所有Worker,即使Worker 正在运行中 privatevoidinterruptWorkers(){ final ReentrantLock mainLock = this.mainLock; mainLock.lock(); try { for (Worker w : workers) w.interruptIfStarted(); } finally { mainLock.unlock(); } }
/** * Interrupts threads that might be waiting for tasks (as * indicated by not being locked) so they can check for * termination or configuration changes. Ignores * SecurityExceptions (in which case some threads may remain * uninterrupted). * * @param onlyOne If true, interrupt at most one worker. This is * called only from tryTerminate when termination is otherwise * enabled but there are still other workers. In this case, at * most one waiting worker is interrupted to propagate shutdown * signals in case all threads are currently waiting. * Interrupting any arbitrary thread ensures that newly arriving * workers since shutdown began will also eventually exit. * To guarantee eventual termination, it suffices to always * interrupt only one idle worker, but shutdown() interrupts all * idle workers so that redundant workers exit promptly, not * waiting for a straggler task to finish. */ // 中断空闲Worker // @param onlyOne 为true,只来自 tryTerminate的调用。 privatevoidinterruptIdleWorkers(boolean onlyOne){ final ReentrantLock mainLock = this.mainLock; mainLock.lock(); try { for (Worker w : workers) { Thread t = w.thread; // 这里获取Worker的锁,说明当前线程处getTask 的wait状态 // Worker 在运行时获取独占锁,在getTask释放锁,这里只中断空闲线程 if (!t.isInterrupted() && w.tryLock()) { try { t.interrupt(); } catch (SecurityException ignore) { } finally { w.unlock(); } } // 只中断一个,返回 if (onlyOne) break; } } finally { mainLock.unlock(); } } privatevoidinterruptIdleWorkers(){ interruptIdleWorkers(false); }
/** * Drains the task queue into a new list, normally using * drainTo. But if the queue is a DelayQueue or any other kind of * queue for which poll or drainTo may fail to remove some * elements, it deletes them one by one. */ private List<Runnable> drainQueue(){ BlockingQueue<Runnable> q = workQueue; List<Runnable> taskList = new ArrayList<Runnable>(); q.drainTo(taskList); if (!q.isEmpty()) { for (Runnable r : q.toArray(new Runnable[0])) { if (q.remove(r)) taskList.add(r); } } return taskList; } /** * Executes the given task sometime in the future. The task * may execute in a new thread or in an existing pooled thread. * * If the task cannot be submitted for execution, either because this * executor has been shutdown or because its capacity has been reached, * the task is handled by the current {@code RejectedExecutionHandler}. * * @param command the task to execute * @throws RejectedExecutionException at discretion of * {@code RejectedExecutionHandler}, if the task * cannot be accepted for execution * @throws NullPointerException if {@code command} is null */ publicvoidexecute(Runnable command){ // 参数校验 if (command == null) thrownew NullPointerException(); /* * Proceed in 3 steps: * * * 1. 如果当前运行的线程数比corePoolSize少, 会尝试启动一个新的线程来执行当前任务。 * 调用addWorker会检查runState 和 workerCount,通过返回 flase 来避免 添加不必要的核心线程 2. 如果一个任务可以成功放入队列,我们仍然需要double-check 来决定是否需要添加一个线程。(因为自从上次checking存在的线程,可能已经死亡)或者进行这个方法时,线程池关闭了。所以,我们需要recheck state 来判断是否需要在stopped时 roll back,或者启动一个新的线程。 * * 3. 如果任务不能放入队列,我们会尝试启动一个新的线程。如果启动失败,我们会知道我们正在关闭或saturated,来拒绝这个任务 */ int c = ctl.get(); if (workerCountOf(c) < corePoolSize) { // worker数小于corePoolSize启动新的Worker if (addWorker(command, true)) return;// 启动新的core线程处理任务成功,直接返回 c = ctl.get(); }//启动core线程失败,继续后续处理 if (isRunning(c) && workQueue.offer(command)) { // 线程池正在运行,且成功添加到workQueue中 int recheck = ctl.get(); if (! isRunning(recheck) && remove(command)) // recheck 当前状态不是运行,则移除并拒绝这个任务 reject(command); elseif (workerCountOf(recheck) == 0) // 当前没有正在执行的Worker,则启动一个新Worker addWorker(null, false); } elseif (!addWorker(command, false)) // 在队列放不下的情况下,启动新的Worker失败(线程数超过maxPoolSize) reject(command); }
/* * Methods for creating, running and cleaning up after workers */
/** * Checks if a new worker can be added with respect to current * pool state and the given bound (either core or maximum). If so, * the worker count is adjusted accordingly, and, if possible, a * new worker is created and started, running firstTask as its * first task. This method returns false if the pool is stopped or * eligible to shut down. It also returns false if the thread * factory fails to create a thread when asked. If the thread * creation fails, either due to the thread factory returning * null, or due to an exception (typically OutOfMemoryError in * Thread#start), we roll back cleanly. * * @param firstTask the task the new thread should run first (or * null if none). Workers are created with an initial first task * (in method execute()) to bypass queuing when there are fewer * than corePoolSize threads (in which case we always start one), * or when the queue is full (in which case we must bypass queue). * Initially idle threads are usually created via * prestartCoreThread or to replace other dying workers. * * @param core if true use corePoolSize as bound, else * maximumPoolSize. (A boolean indicator is used here rather than a * value to ensure reads of fresh values after checking other pool * state). * @return true if successful */ privatebooleanaddWorker(Runnable firstTask, boolean core){ retry: for (;;) { int c = ctl.get(); int rs = runStateOf(c);
// Check if queue empty only if necessary. // runState >= SHUTDOWN // if (rs >= SHUTDOWN && ! (rs == SHUTDOWN && firstTask == null && ! workQueue.isEmpty())) returnfalse;
for (;;) { int wc = workerCountOf(c); if (wc >= CAPACITY || wc >= (core ? corePoolSize : maximumPoolSize)) // 在于线程池的容量返回 // 大于corePoolSize 或 maximumPoolSize 返回 returnfalse; 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 } }
boolean workerStarted = false; boolean workerAdded = false; Worker w = null; try { final ReentrantLock mainLock = this.mainLock; w = new Worker(firstTask);//new一个新的Worker final Thread t = w.thread; if (t != null) { 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 thrownew IllegalThreadStateException(); workers.add(w);// 添加到workers集合 int s = workers.size(); if (s > largestPoolSize)//记录线程最大时的线程数 largestPoolSize = s; workerAdded = true; } } finally { mainLock.unlock(); } if (workerAdded) { t.start();//启动Worker线程 workerStarted = true; } } } finally { // 添加失败,进行worker 清理工作 if (! workerStarted) addWorkerFailed(w); } return workerStarted; }
/** * Rolls back the worker thread creation. * - removes worker from workers, if present * - decrements worker count * - rechecks for termination, in case the existence of this * worker was holding up termination */ privatevoidaddWorkerFailed(Worker w){ final ReentrantLock mainLock = this.mainLock; mainLock.lock(); try { if (w != null) workers.remove(w); decrementWorkerCount(); tryTerminate(); } finally { mainLock.unlock(); } }
/** * Performs cleanup and bookkeeping for a dying worker. Called * only from worker threads. Unless completedAbruptly is set, * assumes that workerCount has already been adjusted to account * for exit. This method removes thread from worker set, and * possibly terminates the pool or replaces the worker if either * it exited due to user task exception or if fewer than * corePoolSize workers are running or queue is non-empty but * there are no workers. * * @param w the worker * @param completedAbruptly if the worker died due to user exception */ privatevoidprocessWorkerExit(Worker w, boolean completedAbruptly){ if (completedAbruptly) // If abrupt, then workerCount wasn't adjusted decrementWorkerCount();
int c = ctl.get(); if (runStateLessThan(c, STOP)) { if (!completedAbruptly) { int min = allowCoreThreadTimeOut ? 0 : corePoolSize; if (min == 0 && ! workQueue.isEmpty()) min = 1; if (workerCountOf(c) >= min) return; // replacement not needed } addWorker(null, false); } }
/** * Performs blocking or timed wait for a task, depending on * current configuration settings, or returns null if this worker * must exit because of any of: * 1. There are more than maximumPoolSize workers (due to * a call to setMaximumPoolSize). * 2. The pool is stopped. * 3. The pool is shutdown and the queue is empty. * 4. This worker timed out waiting for a task, and timed-out * workers are subject to termination (that is, * {@code allowCoreThreadTimeOut || workerCount > corePoolSize}) * both before and after the timed wait. * * @return task, or null if the worker must exit, in which case * workerCount is decremented */ private Runnable getTask(){ boolean timedOut = false; // Did the last poll() time out?
retry: for (;;) { int c = ctl.get(); int rs = runStateOf(c);
// 如果正在 SHUTDOWN 并且 // Check if queue empty only if necessary. if (rs >= SHUTDOWN && (rs >= STOP || workQueue.isEmpty())) { decrementWorkerCount(); returnnull; }
// workers 是否需要判断超时退出 boolean timed; // Are workers subject to culling?
for (;;) { // 判断当前线程数是否需要超时退出 int wc = workerCountOf(c); timed = allowCoreThreadTimeOut || wc > corePoolSize; // 当前线程不需要超时break if (wc <= maximumPoolSize && ! (timedOut && timed)) break; // 递减WorkerCount计数,直接返回null if (compareAndDecrementWorkerCount(c)) returnnull; c = ctl.get(); // Re-read ctl if (runStateOf(c) != rs) continue retry; // else CAS failed due to workerCount change; retry inner loop }
/** * Main worker run loop. Repeatedly gets tasks from queue and * executes them, while coping with a number of issues: * * 1. We may start out with an initial task, in which case we * don't need to get the first one. Otherwise, as long as pool is * running, we get tasks from getTask. If it returns null then the * worker exits due to changed pool state or configuration * parameters. Other exits result from exception throws in * external code, in which case completedAbruptly holds, which * usually leads processWorkerExit to replace this thread. * * 2. Before running any task, the lock is acquired to prevent * other pool interrupts while the task is executing, and * clearInterruptsForTaskRun called to ensure that unless pool is * stopping, this thread does not have its interrupt set. * * 3. Each task run is preceded by a call to beforeExecute, which * might throw an exception, in which case we cause thread to die * (breaking loop with completedAbruptly true) without processing * the task. * * 4. Assuming beforeExecute completes normally, we run the task, * gathering any of its thrown exceptions to send to * afterExecute. We separately handle RuntimeException, Error * (both of which the specs guarantee that we trap) and arbitrary * Throwables. Because we cannot rethrow Throwables within * Runnable.run, we wrap them within Errors on the way out (to the * thread's UncaughtExceptionHandler). Any thrown exception also * conservatively causes thread to die. * * 5. After task.run completes, we call afterExecute, which may * also throw an exception, which will also cause thread to * die. According to JLS Sec 14.20, this exception is the one that * will be in effect even if task.run throws. * * The net effect of the exception mechanics is that afterExecute * and the thread's UncaughtExceptionHandler have as accurate * information as we can provide about any problems encountered by * user code. * * @param w the worker */ finalvoidrunWorker(Worker w){ Thread wt = Thread.currentThread();// 获取当前Worker的执行线程 Runnable task = w.firstTask; w.firstTask = null; w.unlock(); // allow interrupts 允许被中断 boolean completedAbruptly = true; try { // 循环获取任务, getTask 为空时,结束循环,完成当前线程 while (task != null || (task = getTask()) != null) { // 执行任务时,获取独占锁 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();//中断worker线程 try { 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; thrownew Error(x); } finally { afterExecute(task, thrown); } } finally { task = null; w.completedTasks++; w.unlock(); } } completedAbruptly = false; } finally { processWorkerExit(w, completedAbruptly); } }
/** * Initiates an orderly shutdown in which previously submitted * tasks are executed, but no new tasks will be accepted. * Invocation has no additional effect if already shut down. * * <p>This method does not wait for previously submitted tasks to * complete execution. Use {@link #awaitTermination awaitTermination} * to do that. * * @throws SecurityException {@inheritDoc} */ publicvoidshutdown(){ final ReentrantLock mainLock = this.mainLock; mainLock.lock(); try { checkShutdownAccess(); // 设置运行状态为SHUTDOWN advanceRunState(SHUTDOWN); interruptIdleWorkers();//中断所有线程 onShutdown(); // hook for ScheduledThreadPoolExecutor } finally { mainLock.unlock(); } // shutdown后,尝试终止线程池 tryTerminate(); }
/** * Attempts to stop all actively executing tasks, halts the * processing of waiting tasks, and returns a list of the tasks * that were awaiting execution. These tasks are drained (removed) * from the task queue upon return from this method. * * <p>This method does not wait for actively executing tasks to * terminate. Use {@link #awaitTermination awaitTermination} to * do that. * * <p>There are no guarantees beyond best-effort attempts to stop * processing actively executing tasks. This implementation * cancels tasks via {@link Thread#interrupt}, so any task that * fails to respond to interrupts may never terminate. * * @throws SecurityException {@inheritDoc} */ public List<Runnable> shutdownNow(){ List<Runnable> tasks; final ReentrantLock mainLock = this.mainLock; mainLock.lock(); try { checkShutdownAccess(); advanceRunState(STOP); interruptWorkers(); tasks = drainQueue(); } finally { mainLock.unlock(); } // shutdown后,尝试终止线程池 tryTerminate(); return tasks; }
/** * Returns true if this executor is in the process of terminating * after {@link #shutdown} or {@link #shutdownNow} but has not * completely terminated. This method may be useful for * debugging. A return of {@code true} reported a sufficient * period after shutdown may indicate that submitted tasks have * ignored or suppressed interruption, causing this executor not * to properly terminate. * * @return true if terminating but not yet terminated */ publicbooleanisTerminating(){ int c = ctl.get(); return ! isRunning(c) && runStateLessThan(c, TERMINATED); }
publicbooleanawaitTermination(long timeout, TimeUnit unit) throws InterruptedException { long nanos = unit.toNanos(timeout); final ReentrantLock mainLock = this.mainLock; mainLock.lock(); try { for (;;) { if (runStateAtLeast(ctl.get(), TERMINATED)) returntrue; if (nanos <= 0) returnfalse; // 等待 termination nanos = termination.awaitNanos(nanos); } } finally { mainLock.unlock(); } }
/** * Invokes {@code shutdown} when this executor is no longer * referenced and it has no threads. */ protectedvoidfinalize(){ shutdown(); } //******************************预启动线程 *************************
/** * Starts a core thread, causing it to idly wait for work. This * overrides the default policy of starting core threads only when * new tasks are executed. This method will return {@code false} * if all core threads have already been started. * * @return {@code true} if a thread was started */ publicbooleanprestartCoreThread(){ return workerCountOf(ctl.get()) < corePoolSize && addWorker(null, true); }
/** * Same as prestartCoreThread except arranges that at least one * thread is started even if corePoolSize is 0. */ voidensurePrestart(){ int wc = workerCountOf(ctl.get()); if (wc < corePoolSize) addWorker(null, true); elseif (wc == 0) addWorker(null, false); }
/** * Starts all core threads, causing them to idly wait for work. This * overrides the default policy of starting core threads only when * new tasks are executed. * * @return the number of threads started */ publicintprestartAllCoreThreads(){ int n = 0; while (addWorker(null, true)) ++n; return n; }
/** * Removes this task from the executor's internal queue if it is * present, thus causing it not to be run if it has not already * started. * * <p> This method may be useful as one part of a cancellation * scheme. It may fail to remove tasks that have been converted * into other forms before being placed on the internal queue. For * example, a task entered using {@code submit} might be * converted into a form that maintains {@code Future} status. * However, in such cases, method {@link #purge} may be used to * remove those Futures that have been cancelled. * * @param task the task to remove * @return true if the task was removed */ publicbooleanremove(Runnable task){ boolean removed = workQueue.remove(task); // 在 SHUTDOWN 并且队列为空时,尝试终止 tryTerminate tryTerminate(); // In case SHUTDOWN and now empty return removed; }
/** * Tries to remove from the work queue all {@link Future} * tasks that have been cancelled. This method can be useful as a * storage reclamation operation, that has no other impact on * functionality. Cancelled tasks are never executed, but may * accumulate in work queues until worker threads can actively * remove them. Invoking this method instead tries to remove them now. * However, this method may fail to remove tasks in * the presence of interference by other threads. */ publicvoidpurge(){ final BlockingQueue<Runnable> q = workQueue; try { Iterator<Runnable> it = q.iterator(); while (it.hasNext()) { Runnable r = it.next(); if (r instanceof Future<?> && ((Future<?>)r).isCancelled()) it.remove(); } } catch (ConcurrentModificationException fallThrough) { // Take slow path if we encounter interference during traversal. // Make copy for traversal and call remove for cancelled entries. // The slow path is more likely to be O(N*N). for (Object r : q.toArray()) if (r instanceof Future<?> && ((Future<?>)r).isCancelled()) q.remove(r); }
// 在 SHUTDOWN 并且队列为空时,尝试终止 tryTerminate tryTerminate(); // In case SHUTDOWN and now empty }
五、 ExecutorCompletionService
使用提供的 Executor 来执行任务的 CompletionService。此类将把 提交任务完成时的结果 放置 在可使用 take 访问的队列上。该类非常轻便,适合于在执行几组任务时临时使用。