/** DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.** This code is free software; you can redistribute it and/or modify it* under the terms of the GNU General Public License version 2 only, as* published by the Free Software Foundation. Oracle designates this* particular file as subject to the "Classpath" exception as provided* by Oracle in the LICENSE file that accompanied this code.** This code is distributed in the hope that it will be useful, but WITHOUT* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License* version 2 for more details (a copy is included in the LICENSE file that* accompanied this code).** You should have received a copy of the GNU General Public License version* 2 along with this work; if not, write to the Free Software Foundation,* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.** Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA* or visit www.oracle.com if you need additional information or have any* questions.*//** This file is available under and governed by the GNU General Public* License version 2 only, as published by the Free Software Foundation.* However, the following notice accompanied the original version of this* file:** Written by Doug Lea with assistance from members of JCP JSR-166* Expert Group and released to the public domain, as explained at* http://creativecommons.org/publicdomain/zero/1.0/*/package java.util.concurrent;/*** 执行已提交的{@link Runnable}任务的对象。* 此接口提供了一种将任务提交与每个任务的运行机制(包括线程使用,调度的详细信息)解耦的方式。* 通常使用{@code Executor}来代替显式创建线程。* 例如,不用为一组任务中的每一个任务都去调用{@code new Thread(new(RunnableTask())).start()},* 你可以使用:** <pre>* Executor executor = <em>anExecutor</em>;* executor.execute(new RunnableTask1());* executor.execute(new RunnableTask2());* ...* </pre>** 但是,{@code Executor}接口并不严格要求执行是异步的。* 在最简单的情况下,执行器可以在调用者的线程中立即运行提交的任务:** <pre> {@code* class DirectExecutor implements Executor {* public void execute(Runnable r) {* r.run();* }* }}</pre>** 更典型的情况是,任务在调用者线程之外的某个线程中执行。* 下面的执行器为每个任务创建一个新的线程。** <pre> {@code* class ThreadPerTaskExecutor implements Executor {* public void execute(Runnable r) {* new Thread(r).start();* }* }}</pre>** 许多{@code Executor}实现对如何以及何时调度任务施加了某种限制。* 下面的执行器将任务的提交串行化到第二个执行器,演示了组合执行器。** <pre> {@code* class SerialExecutor implements Executor {* final Queue<Runnable> tasks = new ArrayDeque<Runnable>();* final Executor executor;* Runnable active;** SerialExecutor(Executor executor) {* this.executor = executor;* }** public synchronized void execute(final Runnable r) {* tasks.offer(new Runnable() {* public void run() {* try {* r.run();* } finally {* scheduleNext();* }* }* });* if (active == null) {* scheduleNext();* }* }** protected synchronized void scheduleNext() {* if ((active = tasks.poll()) != null) {* executor.execute(active);* }* }* }}</pre>** 这个包中提供的{@code Executor}实现implement了{@link ExecutorService},* 这是一个更加扩展的接口。* {@link ThreadPoolExecutor}类提供了可扩展的线程池实现。* {@link Executors}类提供了更加方便的工厂方法用来生成{@code Executor}。** <p>内存一致性影响: Actions in a thread prior to* submitting a {@code Runnable} object to an {@code Executor}* <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a>* its execution begins, perhaps in another thread.** @since 1.5* @author Doug Lea*/public interface Executor {/*** 在将来的某个时候执行给定的命令。* 该命令可能在新线程、线程池或调用线程中执行,* 具体由{@code Executor}实现决定。** @param command the runnable task* @throws RejectedExecutionException if this task cannot be* accepted for execution* @throws NullPointerException if command is null*/void execute(Runnable command);}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。