Excutor

2024-08-21 00:38
文章标签 excutor

本文主要是介绍Excutor,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

  1. .Excutor 这个只是一个简单的接口 执行程序的意思
public interface Executor {/*** Executes the given command at some time in the future.  The command* may execute in a new thread, in a pooled thread, or in the calling* thread, at the discretion of the {@code Executor} implementation.** @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);
}

这里写图片描述

生成者和消费者的模式,很好的将任务分开来执行,各不影响。 下面的源码中写的东西。
An object that executes submitted {@link Runnable} tasks. This interface provides a way of decoupling(这个是一个解耦的过程,将执行的细节分开了) task submission from the mechanics of how each task will be run, including details of thread use, scheduling, etc. An {@code Executor} is normally used instead of explicitly creating threads. For example, rather than invoking {@code new Thread(new(RunnableTask())).start()} for each of a set of tasks, you might use:

 Executor executor = anExecutor;executor.execute(new RunnableTask1());executor.execute(new RunnableTask2());

However, the {@code Executor} interface does not strictly require that execution be asynchronous. In the simplest case, an executor can run the submitted task immediately in the caller’s thread: 并不是严格的要求使用异步的执行,也是可以是直接使用调用着提供的线程。其实有时候,我们可以把直接写线程的东西,用Executor封装起来,如果每个事务启动的时候都需要创建一个线程,我们直接修改Executor的实现就好了。面向接口编程,不用修改其他的代码。非常的棒。假如下次我们执行的时候不在需要每次都生产一个线程那个时候,我们修改代码很方便啊。很好的扩展我们的需求。

class DirectExecutor implements Executor {public void execute(Runnable r) {r.run();}}}

就如我刚才说的,每次调用的时候都去产生新的线程,这样我们也是可以实现的。

class ThreadPerTaskExecutor implements Executor {public void execute(Runnable r) {new Thread(r).start();}}}

Many {@code Executor} implementations impose some sort of limitation on how and when tasks are scheduled.(有时候对于执行任务有一定的周期的限制,一个接一个的) The executor below serializes the submission of tasks to a second executor, illustrating a composite executor. 这个下面的是源码中的例子,其实我们真正的实现线程池之内的东西,都是按照这种那样的限制进行执行的。生产和消费分开来真的是一个非常不错的东西。其实在我们的平时写代码的时候,解耦。很多的用到了我们的设计模式里面的知识。我自己也是越来越爱上了这个东西。重新买了一本英文的head first pattern 来看看起来太有味道的感觉了。

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);*     }*   }* }}
  1. (Executor)将为你管理Thread对象,从而简化了并发编程。
    Executor在客户端和执行任务之间提供了一个间接层,Executor代替客户端执行任务。Executor允许你管理异步任务的执行,而无须显式地管理线程的生命周期。Executor在Java SE5/6中时启动任务的优选方法。Executor引入了一些功能类来管理和使用线程Thread,其中包括线程池,Executor,Executors,ExecutorService,CompletionService,Future,Callable等
    这里写图片描述

Executors类,提供了一系列工厂方法用于创先线程池,返回的线程池都实现了ExecutorService接口。
这里写图片描述

public static ExecutorService newFixedThreadPool(int nThreads)
创建固定数目线程的线程池。
public static ExecutorService newCachedThreadPool()
创建一个可缓存的线程池,调用execute 将重用以前构造的线程(如果线程可用)。如果现有线程没有可用的,则创建一个新线程并添加到池中。终止并从缓存中移除那些已有 60 秒钟未被使用的线程。
public static ExecutorService newSingleThreadExecutor()
创建一个单线程化的Executor。
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize)
创建一个支持定时及周期性的任务执行的线程池,多数情况下可用来替代Timer类。

接口Executor只有一个方法execute,接口ExecutorService扩展了Executor并添加了一些生命周期管理的方法,如shutdown、submit等。一个Executor的生命周期有三种状态,运行 ,关闭 ,终止。shutdown() 启动一个关闭命令,不再接受新任务,当所有已提交任务执行完后,就关闭。
3. Callable,Future用于返回结果
Future< V>代表一个异步执行的操作,通过get()方法可以获得操作的结果,如果异步操作还没有完成,则,get()会使当前线程阻塞。FutureTask< V>实现了Future< V>和Runable< V>。Callable代表一个有返回值得操作。

public interface Callable<V> {/*** Computes a result, or throws an exception if unable to do so.** @return computed result* @throws Exception if unable to compute a result*/V call() throws Exception;
}
 The {@link FutureTask} class is an implementation of {@code Future} that* implements {@code Runnable}, and so may be executed by an {@code Executor}.* For example, the above construction with {@code submit} could be replaced by:*  <pre> {@code* FutureTask<String> future =*   new FutureTask<String>(new Callable<String>() {*     public String call() {*       return searcher.search(target);*   }});* executor.execute(future);}*
  1. 执行的策略
    这里写图片描述
    这里写图片描述
    这里写图片描述
    这里写图片描述
    这里写图片描述

这些写的都是非常经典的文章,值得好好的品味,编程是一种艺术的生活。得好好的体会和品味。
简单粗暴的关闭和慢慢的等待执行完任务..给我们解释的非常的清楚,有时候多看看前辈的经验还是十分的有效的一种方式。
这里写图片描述

这篇关于Excutor的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1091628

相关文章

你不知道的Promise构造函数Promise(excutor)

Promise构造函数Promise(excutor)// 说明一下:excutor会在Promise内部立刻同步调用;(异步操作在执行器执行)var p = new Promise((resolve, reject) => {// resolve 既是函数也是参数,它用于处理成功的; 在异步任务成功的时候,去调用resolve// reject 既是函数也是参数,它用于处理失败的; 在异步任务

Spark源码分析之Excutor资源分配流程

一.前言 在用户提交应用程序时,SparkContext会向Master发送注册消息,并由Master给该应用分配Executor。 这里的SparkContext主要用于负责和ClusterManager通信,进行资源的管理,任务分配和监控,负责作业执行的生命周期管理,ClusterManager提供了资源的分配和管理。 在不同模式下ClusterManager的角色不同,Standalone