方便使用Executors.newSingleThreadExecutor()的示例 [英] Examples of when it is convenient to use Executors.newSingleThreadExecutor()

查看:770
本文介绍了方便使用Executors.newSingleThreadExecutor()的示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

能否请别人告诉我一个真实的例子,在该例子中使用此工厂方法比使用其他工厂方法更方便?

Could please somebody tell me a real life example where it's convenient to use this factory method rather than others?

newSingleThreadExecutor

newSingleThreadExecutor

公共静态ExecutorService newSingleThreadExecutor()

public static ExecutorService newSingleThreadExecutor()

创建一个执行程序,该执行程序使用单个工作线程在 无限队列. (但是请注意,如果该单线程终止 由于关机前执行期间发生故障,因此将有一个新的 如果需要执行后续任务,请取而代之.) 保证按顺序执行,并且最多只能执行一项任务 在任何给定时间都处于活动状态.与其他等效项不同 newFixedThreadPool(1)保证返回的执行者不会 可重新配置以使用其他线程.

Creates an Executor that uses a single worker thread operating off an unbounded queue. (Note however that if this single thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks.) Tasks are guaranteed to execute sequentially, and no more than one task will be active at any given time. Unlike the otherwise equivalent newFixedThreadPool(1) the returned executor is guaranteed not to be reconfigurable to use additional threads.

谢谢.

推荐答案

能否请别人告诉我一个真实的示例,在该示例中,使用[newSingleThreadExecutor()工厂方法]比使用其他方法更方便?

Could please somebody tell me a real life example where it's convenient to use [the newSingleThreadExecutor() factory method] rather than others?

我假设您是在询问何时使用单线程线程池而不是固定线程或缓存线程池.

I assume you are asking about when you use a single-threaded thread-pool as opposed to a fixed or cached thread pool.

当我有许多任务要运行时,我只使用一个线程执行器,但是我只希望一个线程来执行它.这与使用1的固定线程池相同.通常这是因为我们不需要它们并行运行,它们是后台任务,并且我们不想占用过多的系统资源(CPU,内存,IO).我想将各种任务处理为CallableRunnable对象,因此ExecutorService是最佳选择,但我只需要一个线程即可运行它们.

I use a single threaded executor when I have many tasks to run but I only want one thread to do it. This is the same as using a fixed thread pool of 1 of course. Often this is because we don't need them to run in parallel, they are background tasks, and we don't want to take too many system resources (CPU, memory, IO). I want to deal with the various tasks as Callable or Runnable objects so an ExecutorService is optimal but all I need is a single thread to run them.

例如,我有很多春天要注入的计时器任务.我有两种任务,我的短期"任务在单个线程池中运行.即使我的系统中有数百个线程,也只有一个线程可以执行所有这些线程.他们执行例行任务,例如检查磁盘空间,清理日志,转储统计信息等.对于时间紧迫的任务,我在高速缓存的线程池中运行.

For example, I have a number of timer tasks that I spring inject. I have two kinds of tasks and my "short-run" tasks run in a single thread pool. There is only one thread that executes them all even though there are a couple of hundred in my system. They do routine tasks such as checking for disk space, cleaning up logs, dumping statistics, etc.. For the tasks that are time critical, I run in a cached thread pool.

另一个例子是我们有一系列合作伙伴集成任务.它们并不需要很长的时间,而且它们很少运行,我们不希望它们与其他系统线程竞争,因此它们可以在单个线程执行器中运行.

Another example is that we have a series of partner integration tasks. They don't take very long and they run rather infrequently and we don't want them to compete with other system threads so they run in a single threaded executor.

第三个例子是我们有一个有限状态机,其中每个状态转换器将作业从一个状态转移到另一个状态,并在单个线程池中注册为Runnable.即使我们有数百个mutator,在任何一个时间点都只有一个任务有效,因此为该任务分配多个线程是没有意义的.

A third example is that we have a finite state machine where each of the state mutators takes the job from one state to another and is registered as a Runnable in a single thread-pool. Even though we have hundreds of mutators, only one task is valid at any one point in time so it makes no sense to allocate more than one thread for the task.

这篇关于方便使用Executors.newSingleThreadExecutor()的示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆