java Executors.newSingleThreadExecutor().execute(command) 和 new Thread(command).start() 的区别;
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1960832/
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
The difference between Executors.newSingleThreadExecutor().execute(command) and new Thread(command).start();
提问by skaffman
Well title says it, what is the difference between Executors.newSingleThreadExecutor().execute(command)and new Thread(command).start();
好标题说,什么是之间的区别Executors.newSingleThreadExecutor().execute(command)和new Thread(command).start();
回答by skaffman
Behaviourally, pretty much nothing.
在行为上,几乎没有。
However, once you have an Executorinstance, you can submit multiple tasks to it, and have them executed one after another. You can't do that simply with a raw Thread.
然而,一旦你有了一个Executor实例,你就可以向它提交多个任务,并让它们一个接一个地执行。你不能简单地用一个 raw 来做到这一点Thread。
回答by John Vint
One noticeable difference, is when you run new Thread(someRunnable).start();when the runnable is finished the thread will die quietly.
一个明显的区别是,当您new Thread(someRunnable).start();在可运行对象完成时运行时,线程将安静地死亡。
The Executor though will persist until you shut it down. So running Executors.newSingleThreadExecutor().execute(command)When you think your application or the JVM may be finished the Executor may still be running in a background thread.
Executor 会一直存在,直到你关闭它。所以运行Executors.newSingleThreadExecutor().execute(command)当您认为您的应用程序或 JVM 可能已完成时,Executor 可能仍在后台线程中运行。
回答by Peter Lawrey
With Executor.execute, if an Erroror RuntimeExceptionis thrown in the Executorit will be swallowed silently, while the new Thread()will print it to System.err.
With Executor.execute,如果一个ErrororRuntimeException被扔进去,Executor它会被默默吞下,而new Thread()会打印到System.err。
回答by Junaed
There can be many differences, but I will show you one difference which I found very important to understand:
可能有很多差异,但我将向您展示一个差异,我认为理解这一点非常重要:
public void sendEventSingleThreadExecutor(Event e){
Executor.singleThreadExecutor().execute(()->{//send the event here})
}
Now, even if you callsendEventSingleThreadExecutor method 10 times, it will use only a single thread to sendthem. It will not create a new thread every time. Which means that the events will be sent sequentially or synchronously! You can read more from here:
现在,即使您调用sendEventSingleThreadExecutor 方法10 次,它也只会使用单个线程来发送它们。它不会每次都创建一个新线程。这意味着事件将按顺序或同步发送!您可以从这里阅读更多信息:
Now see the below example with new thread
现在用新线程查看下面的示例
public void sendEventThread(Event e){
Thread(//send the event here).start();
}
If you call it 10 times, it will create 10 new threads. Which means, the executions will be asynchronous! And it could be dangerous, it can create a lot of threads depending on how many times you call sendEventThread functions.
如果您调用它 10 次,它将创建10 个新线程。这意味着,执行将是异步的!它可能很危险,它可以根据您调用 sendEventThread 函数的次数创建大量线程。
Please note that, the code are only for demonstration purpose, it might have syntax error!If you find any wrong description here, I will be happy to be corrected.
请注意,代码仅用于演示目的,可能存在语法错误!如果您在此处发现任何错误的描述,我将很乐意予以纠正。
Some more information from here
来自这里的更多信息
newSingleThreadExecutor. A single-threaded executor creates a single worker thread to process tasks, replacing it if it dies unexpectedly. Tasks are guaranteed to be processed sequentially according to the order imposed by the task queue (FIFO, LIFO, priority order).[4]
[4] Single-threaded executors also provide sufficient internal synchronization to guarantee that any memory writes made by tasks are visible to subsequent tasks; this means that objects can be safely confined to the “task thread” even though that thread may be replaced with another from time to time.
新的单线程执行器。单线程执行器创建一个单独的工作线程来处理任务,如果它意外死亡,则替换它。任务保证按照任务队列规定的顺序(先进先出、后进先出、优先级顺序)依次处理。 [4]
[4] 单线程执行器还提供了足够的内部同步,以保证任务所做的任何内存写入对后续任务都是可见的;这意味着对象可以安全地限制在“任务线程”中,即使该线程可能会不时被另一个线程替换。
回答by Sachin Singh
Executors.newSingleThreadExecutor().execute(command) will reuse previously constructed thread, it will not created new thread as in case of new Thread(). If the thread that have not been used for sixty seconds are terminated, It's a kind of pool which contains a single thread which make its equivalent newFixedThreadPool(1).
Executors.newSingleThreadExecutor().execute(command) 将重用先前构造的线程,它不会像 new Thread() 那样创建新线程。如果 60 秒未使用的线程被终止,它是一种包含单个线程的池,使其等效为 newFixedThreadPool(1)。
回答by Ravindra babu
I prefer to use ExecutorServiceor ThreadPoolExecutoreven for single digit threads. They offer more flexibility.
我更喜欢使用ExecutorService或ThreadPoolExecutor什至用于个位数线程。它们提供了更大的灵活性。
Have a look at ExecutorService& ThreadPoolExecutorsections in related SE questions :
查看相关 SE 问题中的ExecutorService&ThreadPoolExecutor部分:
java Fork/Join pool, ExecutorService and CountDownLatch
java Fork/Join 池、ExecutorService 和 CountDownLatch
Java's Fork/Join vs ExecutorService - when to use which?
Java 的 Fork/Join 与 ExecutorService - 何时使用哪个?
Assume that you have started with your own thread instead of ExecutorService. In future, if there is a need for supporting multiple threads, ExecutorServiceor ThreadPoolExecutorwill offer better control and flexibility for you. You can fine-tune required number of parameters in these below APIs.
假设您已经开始使用自己的线程而不是ExecutorService. 以后如果有需要支持多线程,ExecutorService或者ThreadPoolExecutor会为你提供更好的控制和灵活性。您可以在以下这些 API 中微调所需数量的参数。
ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime,
TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory,
RejectedExecutionHandler handler)

