Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SingleThreadExecutor VS plain thread

Apart from the fact that the Executor interface has some advantages over plain threads (management, for example), is there any real internal difference (big performance difference, resource consumption...) between doing:

ExecutorService executor = Executors.newSingleThreadExecutor();
executor.submit(runnable);

And:

Thread thread = new Thread(runnable);
thread.start();

I'm only asking about a single thread here.

like image 254
Magd Kudama Avatar asked Apr 28 '18 13:04

Magd Kudama


People also ask

What is Singlethreadexecutor?

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.).

What is the difference between thread and Executor?

A Thread represents something which is responsible for executing your code in parallel, while an Executor is an abstraction for concurrent task execution.

What is use of single thread pool?

Single Thread Executor. 1. Basic. Creates a thread pool that can schedule commands to run after a given delay, or to execute periodically. Tasks are guaranteed to execute sequentially, and no more than one task will be active at any given time.

What is thread pool and Executor?

ThreadPoolExecutor is an ExecutorService to execute each submitted task using one of possibly several pooled threads, normally configured using Executors factory methods. It also provides various utility methods to check current threads statistics and control them.


Video Answer


2 Answers

Executors#newSingleThreadExecutor() creates ThreadPoolExecutor object under the hood,
see the code here: http://www.docjar.com/html/api/java/util/concurrent/Executors.java.html

  133       public static ExecutorService newSingleThreadExecutor() {
  134           return new FinalizableDelegatedExecutorService
  135               (new ThreadPoolExecutor(1, 1,
  136                                       0L, TimeUnit.MILLISECONDS,
  137                                       new LinkedBlockingQueue<Runnable>()));
  138       }

The documentation of ThreadPoolExecutor explains in what situations it gives advantages:

Thread pools address two different problems: they usually provide improved performance when executing large numbers of asynchronous tasks, due to reduced per-task invocation overhead, and they provide a means of bounding and managing the resources, including threads, consumed when executing a collection of tasks. Each ThreadPoolExecutor also maintains some basic statistics, such as the number of completed tasks.

If all you need is to just run single thread only once in a while (say once an hour), then in terms of performance, using ThreadPoolExecutor may be slower, since you need to instantiate the whole machinery (pool + thread), then throw it away from memory.

But if you want to use this single thread often (say every 15 seconds), then the advantage is that you create the pool and thread only once, keeping it in memory, and use it all the time saving time creating a new thread every now and then (which might be quite expensive, if you want to use it say every 15 seconds or so).

like image 59
krokodilko Avatar answered Oct 22 '22 04:10

krokodilko


The major difference is in task execution policy.

By creating a Thread instance or subclassing Thread you are basically executing a single task.

Using Executors.newSingleThreadExecutor() on the other hand allows you to submit multiple tasks. Since those tasks are guaranteed not to be executed concurrently, this allows you to exploit the following thread confinement benefits:

  • No synchronization required when accessing objects that are not thread-safe
  • Memory effects of one task are guaranteed to be visible to the next task
like image 9
xuesheng Save Ukraine Avatar answered Oct 22 '22 04:10

xuesheng Save Ukraine