简体   繁体   English

java中的ExecutorService执行方法

[英]ExecutorService execute method in java

According to Runnable execute method in Executor , corresponding JAVA doc mentions that it:根据Executor Runnable execute方法,相应的 JAVA 文档提到它:

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.该命令可以在新线程、池线程或调用线程中执行,具体取决于 {@code Executor} 实现。

My doubt is, for example if we create executor service as我的疑问是,例如,如果我们将执行程序服务创建为

ExecutorService executor = Executors.newFixedThreadPool(5);

and create a Runnable task and run it by the above execute method as:并创建一个Runnable任务并通过上面的execute方法运行它:

Runnable testRunnableTask = () ->  {
      Runnable testRunnableTask = () ->  {
          System.out.println("my tested function");
      } 
 };
executor.execute(testRunnableTask); 

Then this testRunnableTask should be executed by one of the threads that we have initialized through our fixed thread pool.然后这个testRunnableTask应该由我们通过固定线程池初始化的线程之一执行。 But as per JAVA doc it can be executed by new thread or thread pool or calling thread.但根据 JAVA 文档,它可以由新线程或线程池或调用线程执行。

If my understanding is correct of JAVA doc, then my doubt is why execute method doesn't always use one of the threads from the pool like other methods of ExecutorService like submit .如果我对 JAVA 文档的理解是正确的,那么我的疑问是为什么execute方法并不总是像ExecutorService其他方法一样使用池中的一个线程,例如submit

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 .该命令可以在新线程、池线程或调用线程中执行,具体取决于 {@code Executor} 实现

So, basically this means that how a Runnable or Callable task works depends on the Executor overall implementation, not the specific method execution.所以,基本上这意味着RunnableCallable任务的工作方式取决于Executor整体实现,而不是具体的方法执行。 Executor is just an abstraction which provides an API to execute some lambda or task in a way that is up to the implementation. Executor只是一个抽象,它提供了一个 API,以一种取决于实现的方式执行某些 lambda 或任务。

So in your particular case executor.execute(testRunnableTask);所以在你的特殊情况下executor.execute(testRunnableTask); because you are using newFixedThreadPool then testRunnableTask will be executed in one of 5 pre-initialized threads.因为您使用的是newFixedThreadPool然后testRunnableTask将在 5 个预初始化线程之一中执行。 If you would use Executors.newCachedThreadPool() , the task might be executed either in new or cached thread.如果您使用Executors.newCachedThreadPool() ,则任务可能会在新线程或缓存线程中执行。 And so on.等等。

Hope this helps!希望这可以帮助!

submit will submit a task for execution by execute . submit将提交一个任务以供execute In other words, submit internally calls execute to execute the task.也就是说, submit内部调用execute来执行任务。

How execute will execute the task depends on the configuration or constructor you use for ThreadPoolExecutor execute将如何execute任务取决于您用于ThreadPoolExecutor的配置或构造函数

public ThreadPoolExecutor(int corePoolSize,
                          int maximumPoolSize,
                          long keepAliveTime,
                          TimeUnit unit,
                          BlockingQueue<Runnable> workQueue,
                          ThreadFactory threadFactory,
                          RejectedExecutionHandler handler)
  1. Idle threads will die if the total number of threads is more than corePoolSize如果线程总数超过corePoolSize空闲线程将会死亡
  2. If the number of tasks submitted are more than corePoolSize , new threads will be created until maximumPoolSize is reached如果提交的任务数超过corePoolSize ,则将创建新线程,直到达到maximumPoolSize
  3. handler implementation gives a saturation policy that determines what wil happen when all the threads are executing tasks and the bounded blocking queue is also full. handler实现给出了一个饱和策略,该策略确定当所有线程都在执行任务并且有界阻塞队列也已满时会发生什么。 If the handler implementation passed is ThreadPoolExecutor.CallerRunsPolicy , the caller thread will execute the task如果传递的处理程序实现是ThreadPoolExecutor.CallerRunsPolicy ,则调用者线程将执行任务
  4. newFixedThreadPool is a specialized instance of ThreadPoolExecutor as given below. newFixedThreadPoolThreadPoolExecutor一个特殊实例,如下所示。 The corePoolSize and maximumPoolSize are same. corePoolSizemaximumPoolSize相同。 The saturation policy is not provided - hence the default policy ThreadPoolExecutor.AbortPolicy is used.未提供饱和策略 - 因此使用默认策略ThreadPoolExecutor.AbortPolicy
return new ThreadPoolExecutor(nThreads, nThreads,
                                      0L, TimeUnit.MILLISECONDS,
                                      new LinkedBlockingQueue<Runnable>());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM