[英]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.所以,基本上这意味着Runnable
或Callable
任务的工作方式取决于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)
corePoolSize
如果线程总数超过corePoolSize
空闲线程将会死亡corePoolSize
, new threads will be created until maximumPoolSize
is reached如果提交的任务数超过corePoolSize
,则将创建新线程,直到达到maximumPoolSize
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
,则调用者线程将执行任务newFixedThreadPool
is a specialized instance of ThreadPoolExecutor
as given below. newFixedThreadPool
是ThreadPoolExecutor
一个特殊实例,如下所示。 The corePoolSize
and maximumPoolSize
are same. corePoolSize
和maximumPoolSize
相同。 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.