Java newSingleThreadScheduledExecutor 的工作,如果线程已经很忙
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21436761/
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
Working of newSingleThreadScheduledExecutor, if thread already busy
提问by reiley
My requirement is to make a service which keeps on checking a queue after specific interval & process the elements in queue.
我的要求是制作一个服务,在特定时间间隔后继续检查队列并处理队列中的元素。
For scheduling task after 10 seconds, I'm using:
对于 10 秒后的调度任务,我正在使用:
ScheduledExecutorService schd = Executors.newSingleThreadScheduledExecutor();
schd.scheduleAtFixedRate(readQueueRunnable, 10, 10, TimeUnit.SECONDS);
My question is suppose first time, in queue there are many elements and my single thread starts processing the queue.
我的问题是假设第一次,队列中有很多元素,我的单线程开始处理队列。
Even after 10 seconds my first thread is still executing it.
即使 10 秒后,我的第一个线程仍在执行它。
So when after 10 seconds, runnable is called again, will it stop the previous executing thread and start the new one. Or, it will check if thread is already running & if it is running then it will skip going inside Runnable in that case.
因此,当 10 秒后再次调用 runnable 时,它会停止前一个正在执行的线程并启动新线程。或者,它会检查线程是否已经在运行,如果它正在运行,那么在这种情况下它将跳过进入 Runnable 。
回答by zapl
newSingleThreadScheduledExecutor();
creates an executor with a single thread that will never do anything in parallel to the existing thread. It has just one and threads can't execute more than 1 thing at a time.
newSingleThreadScheduledExecutor();
创建一个带有单个线程的执行程序,该线程永远不会与现有线程并行执行任何操作。它只有一个,线程一次不能执行多于 1 件事。
If your tasks takes longer than 10 seconds, it will still create a new task after 10 seconds and put it in the queue of tasks that await completion. If tasks always take longer than the rate at which you schedule them you will get a constantly growing task queue and probably a memory related crash at some point.
如果您的任务耗时超过 10 秒,它仍会在 10 秒后创建一个新任务并将其放入等待完成的任务队列中。如果任务总是比你安排它们的速度花费更长的时间,你将得到一个不断增长的任务队列,并且可能在某个时候与内存相关的崩溃。
So when after 10 seconds, runnable is called again, will it stop the previous executing thread and start the new one.
因此,当 10 秒后再次调用 runnable 时,它会停止前一个正在执行的线程并启动新线程。
It will not stop anything. Executors re-use their thread. The threads executes all Runnable
that you give to it in sequence. So it will simply execute the next runnable once the first is done.
它不会阻止任何事情。执行者重用他们的线程。线程按Runnable
顺序执行您提供给它的所有内容。因此,一旦第一个完成,它将简单地执行下一个 runnable。
Or, it will check if thread is already running & if it is running then it will skip going inside Runnable in that case.
或者,它会检查线程是否已经在运行,如果它正在运行,那么在这种情况下它将跳过进入 Runnable 。
It will not skip creating a task after 10 seconds.
它不会在 10 秒后跳过创建任务。
The documentationexplains it like
该文档解释它像
If any execution of this task takes longer than its period, then subsequent executions may start late, but will not concurrently execute.
如果此任务的任何执行时间超过其周期,则后续执行可能会延迟开始,但不会并发执行。
The "not concurrently" part is meant for executors that have multiple threads. Not relevant here since that can't happen.
“非并发”部分适用于具有多个线程的执行程序。在这里不相关,因为那不可能发生。
If you want that there is always a 10 second delay betweentasks, use scheduleWithFixedDelay
如果您希望任务之间始终有 10 秒的延迟,请使用scheduleWithFixedDelay