Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The difference between Executors.newSingleThreadExecutor().execute(command) and new Thread(command).start();

People also ask

What is executors newSingleThreadExecutor?

The newSingleThreadExecutor() method of Executors class 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 before the shutdown, a new one will take its place if needed to execute subsequent tasks.).

Does newSingleThreadExecutor create a new thread?

newSingleThreadExecutor. Creates an Executor that uses a single worker thread operating off an unbounded queue, and uses the provided ThreadFactory to create a new thread when needed.

What is the difference between Executor and ExecutorService?

Executor just executes stuff you give it. ExecutorService adds startup, shutdown, and the ability to wait for and look at the status of jobs you've submitted for execution on top of Executor (which it extends). This is a perfect answer, short and clear.


Behaviourally, pretty much nothing.

However, once you have an Executor instance, you can submit multiple tasks to it, and have them executed one after another. You can't do that simply with a raw Thread.


One noticeable difference, is when you run new Thread(someRunnable).start(); when the runnable is finished the thread will die quietly.

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.


With Executor.execute, if an Error or RuntimeException is thrown in the Executor it will be swallowed silently, while the new Thread() will print it to System.err.


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 call sendEventSingleThreadExecutor method 10 times, it will use only a single thread to send them. 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:

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.

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.