How many Threads are created when passing tasks to an Executor Instance?

How many Threads are created when passing tasks to an Executor Instance?

How many Threads are created when passing tasks to an Executor Instance?

A.
A new Thread is used for each task.

B.
A number of Threads equal to the number of CPUs is used to execute tasks.

C.
A single Thread is used to execute all tasks.

D.
A developer-defined number of Threads Is used to execute tasks.

E.
A number of Threads determined by system load is used to execute tasks.

F.
The method used to obtain the Executor determines how many Threads are used to execute tasks.

Explanation:
A simple way to create an executor that uses a fixed thread pool is to invoke the newFixedThreadPool factory method in java.util.concurrent.Executors This class also provides the following factory methods:
* The newCachedThreadPool method creates an executor with an expandable thread pool. This executor is suitable for applications that launch many short-lived tasks.
* The newSingleThreadExecutor method creates an executor that executes a single task at a time.
* Several factory methods are ScheduledExecutorService versions of the above executors.
If none of the executors provided by the above factory methods meet your needs, constructing instances of java.util.concurrent.ThreadPoolExecutor or java.util.concurrent.ScheduledThreadPoolExecutor will give you additional options.
Note:The Executor interface provides a single method, execute, designed to be a drop-in replacementfor a common thread-creation idiom. If r is a Runnable object, and e is an Executor object you can replace (new Thread(r)).start();
with e.execute(r);
However, the definition of execute is less specific. The low-level idiom creates a new thread and launches it immediately. Depending on the Executor implementation, execute may do the same thing, but is more likely to use an existing worker thread to run r, or to place r in a queue to wait for a worker thread to become available.
Reference: The Java Tutorials,Thread Pools
Reference: The Java Tutorials,Executor Interfaces



Leave a Reply 0

Your email address will not be published. Required fields are marked *