英文:
What is benefit of SingleThreadPool over synchronization?
问题
我在学习ExecutorService。
我发现有4种类型的线程池。
-
FixedThreadPool
int coreCount = Runtime.getRuntime().availableProcessors(); ExecutorService service = Executors.newFixedThreadPool(coreCount);
-
CachedThreadPool
ExecutorService service = Executors.newCachedThreadPool();
在这里,如果没有空闲线程,则会自动创建线程,并且在线程处于空闲状态时由JVM删除线程。线程存储在队列中。
-
ScheduledThreadPool
线程存储在延迟队列中,该队列可以根据计划的发生时间对元素进行排序。
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(10); // 在10秒延迟后运行任务 scheduledExecutorService.schedule(new Task(), 10, TimeUnit.SECONDS); // 每隔10秒重复运行任务 scheduledExecutorService.scheduleAtFixedRate(new Task(), 15, 10, TimeUnit.SECONDS); // 在上一个任务完成后,每隔10秒重复运行任务 scheduledExecutorService.scheduleWithFixedDelay(new Task(), 15, 10, TimeUnit.SECONDS);
-
SingleThreadedExecutor
当您需要按顺序运行线程时,应该使用SingleThreadPoolService。
ExecutorService service = Executors.newSingleThreadExecutor();
所以我的问题是,Single Thread Executor看起来与同步操作非常相似。有人能否描述两者之间的任何区别。在同步操作方面有什么好处。为什么要引入Single Thread Executor?意味着为什么要使用线程?
英文:
I was studing about ExecutorService.
I found that there are 4 types of Thread pools. <br/>
1. FixedThreadPool <br/>
int coreCount = Runtime.getRuntime().availableProcessors();
ExecutorService service = Executors.newFixedThreadPool(coreCount);
- CachedThreadPool<br/>
ExecutorService service = Executors.newCachedThreadPool();
here threads are automatically created if no created thread is free and deleted when a thread is idle by JVM.
Threads are stored in queue.
- ScheduledThreadPool<br/>
Threads are stored in a delay queue
this queue can sort the element based on their scheduled occurrence. <br/>
Service.schedule<br>
Service.scheduleAtFixedRate<br>
Service.scheduleAtFixedDelay<br>
Schedule the tasks to run based on time delay (and retrigger for fixedRate / fixedDelay)<br/>
Life Cycle: More threads are created if required.
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool ( 10);
// task to run after 10 second delay
scheduledExecutorService.schedule(new Task(), 10, TimeUnit.SECONDS);
// task to run repeatedly every 10 seconds
scheduledExecutorService.scheduleAtFixedRate(new Task(), 15, 10, TimeUnit.SECONDS);
// task to run repeatedly 10 seconds after previous task completes
scheduledExecutorService.scheduleWithFixedDelay(new Task(), 15, 10, TimeUnit.SECONDS);
- SingleThreadedExecutor<br/>
when you require to run thread in a sequential order you should use SingleThreadPoolService.<br/>
ExecutorService service = Executors.newSingleThreadExecutor();
So my question is the single Thread Executor looks very similar to Synchronized operations. Can anyone please describe any difference between those two. any benefit over synchronization. What was the motivation for a Single Thread executor ? means why thread at all ?
答案1
得分: 1
你可以使用一个线程和一些同步代码来实现类似于单线程执行器的功能,但这需要大量的工作。例如,有一些事情你必须手动实现:
- 任务队列。任务提交(而不是执行)到执行器会立即执行,任务将被存储以供以后执行。每次调用同步块都会等待,因此你必须手动存储任务以实现类似的行为。
- 任务执行的顺序。
- 将任务提交给执行器会返回一个 Future。
因此,请查看执行器的 API,并考虑如何手动实现它,你会发现有很多工作要做。
英文:
You can implement analog of single thread executor with a Thread and some synchronized code, however it would take lots of effort. For example, some things you have to implement manually:
- A queue of tasks. Task submission (not execution) to an executor would be performed immediately, task would be stored for later execution. Every call to synchronized block would wait, so you have to store task manually in order to archive similar behaviour.
- Order of task execution.
- Task submission to an executor returns a Future
So look at a executors API and think how could you implement it manually and you see a lot of work to do.
答案2
得分: -1
SingleThreadExecutor确保所有请求由单个线程执行。想象一个物理售货亭,顾客在队列中下订单。
英文:
SingleThreadExecutor makes sure all requests gets executed by a single thread. Imagine a physical kiosk taking orders from customers in queue.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论