Spring ThreadPoolTask​​Executor vs Java Executorservice cachedthreadpool

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24903658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 15:30:13  来源:igfitidea点击:

Spring ThreadPoolTaskExecutor vs Java Executorservice cachedthreadpool

javaspringconcurrency

提问by BalaB

What are the pros and cons of using

使用的优缺点是什么

Spring ThreadPoolTaskExecutorvs Java Executorservicecachedthreadpooleven though spring is wrapper of Java concurrency.

Spring ThreadPoolTask​​Executorvs Java Executorservicecachedthreadpool即使 spring 是 Java 并发的包装器。

Just would like to know the flexibility in using them.

只是想知道使用它们的灵活性。

采纳答案by Pramod S. Nikam

One of the added Advantage of using ThreadPoolTaskExecutorof spring is that it is well suited for management and monitoring (e.g. through JMX), providing several useful attributes: "corePoolSize", "maxPoolSize", "keepAliveSeconds" (all supporting updates at runtime); "poolSize", "activeCount".

使用spring 的ThreadPoolTask​​Executor的附加优势之一 是它非常适合管理和监控(例如通过 JMX),提供了几个有用的属性:“corePoolSize”、“maxPoolSize”、“keepAliveSeconds”(都支持运行时更新);“poolSize”,“activeCount”。

apart from that it is obviously simple to use if you already have spring injections implemented in your application. by using it you can directly inject thread pool by setter injection like below:

除此之外,如果您已经在应用程序中实现了 spring 注入,那么使用起来显然很简单。通过使用它,您可以通过 setter 注入直接注入线程池,如下所示:

<bean id="taskExecutor"
    class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
    <property name="corePoolSize" value="5" />
    <property name="maxPoolSize" value="10" />
    <property name="WaitForTasksToCompleteOnShutdown" value="true" />
</bean> 

ThreadPoolTaskExecutor Doc

ThreadPoolTask​​Executor 文档

on the other hand ExecutorService CachedThreadPoolis good utility to share your most recent under utilized threads ( Under 60 seconds). It is important to point out that CachedThreadPool is not separate class its method ( newCachedThreadPool() ) .

另一方面,ExecutorService CachedThreadPool是一个很好的实用工具,可以共享您最近未充分利用的线程(低于 60 秒)。重要的是要指出 CachedThreadPool 不是单独的类,它的方法 ( newCachedThreadPool() ) 。

CachedThreadPool Doc

缓存线程池文档

回答by mkazma

After Googling you will get the following:

谷歌搜索后,您将获得以下信息:

Executorservice

执行服务

The java.util.concurrent.ExecutorService interface represents an asynchronous execution mechanism which is capable of executing tasks in the background. An ExecutorService is thus very similar to a thread pool. In fact, the implementation of ExecutorService present in the java.util.concurrent package is a thread pool implementation.

java.util.concurrent.ExecutorService 接口表示一种能够在后台执行任务的异步执行机制。因此,ExecutorService 与线程池非常相似。实际上,java.util.concurrent 包中的 ExecutorService 实现是一个线程池实现。

ThreadPoolTaskExecutor

线程池任务执行器

This implementation can only be used in a Java 5 environment but is also the most commonly used one in that environment. It exposes bean properties for configuring a java.util.concurrent.ThreadPoolExecutor and wraps it in a TaskExecutor. If you need something advanced such as a ScheduledThreadPoolExecutor, it is recommended that you use a ConcurrentTaskExecutor instead.

此实现只能在 Java 5 环境中使用,但也是该环境中最常用的一种。它公开用于配置 java.util.concurrent.ThreadPoolExecutor 的 bean 属性并将其包装在 TaskExecutor 中。如果您需要诸如 ScheduledThreadPoolExecutor 之类的高级功能,建议您改用 ConcurrentTaskExecutor。