标签:des style blog ar io color os sp for
public interface Executor { void execute(Runnable command); }
虽然Executor是一个简单的接口,但它为灵活且强大的异步任务框架提供了基础,该框架能支持多种不同类型的任务执行策略。它提供了一种标准的方法将任务的提交过程与执行过程解耦开来,并用Runable来表示任务。Executor的实现还提供了对生命周期的支持,以及统计信息收集,应用程序管理机制和性能检测等机制。
Executor is based on the producer-consumer pattern, where activities that submit tasks are the producers (producing units of work to be done) and the threads that execute tasks are the consumers (consuming those units of work).
应用服务器的中程序:
class TaskExecutionWebServer { private static final int NTHREADS = 100; private static final Executor exec = Executors.newFixedThreadPool(NTHREADS); private static final Executor exec1 = new ThreadPerTaskExecutor(); private static final Executor exec2 = new WithinThreadExecutor(); public static void main(String[] args) throws IOException { ServerSocket socket = new ServerSocket(80); while (true) { final Socket connection = socket.accept(); Runnable task = new Runnable() { public void run() { // handleRequest(connection); } }; exec.execute(task); } } } class ThreadPerTaskExecutor implements Executor { public void execute(Runnable r) { new Thread(r).start(); }; } class WithinThreadExecutor implements Executor { public void execute(Runnable r) { r.run(); }; }
其中的exec,线程是作为线程池的方式执行。exec1,每次的连接作为一个线程来进行执行。exec2,作为同步的机制下执行线程。
Execution policies are a resource management tool, and the optimal policy depends on the available computing resources and your quality-of-service requirements. By limiting the number of concurrent tasks, you can ensure that the application does not fail due to resource exhaustion or suffer performance problems due to contention for scarce resources.[3] Separating the specification of execution policy from task submission makes it practical to select an execution policy at deployment time that is matched to the available hardware.
标签:des style blog ar io color os sp for
原文地址:http://www.cnblogs.com/zhailzh/p/4168238.html