Difference Betwixt Executorservice.Submit() As Well As Executor.Execute() Methods Inwards Java?
What is the divergence betwixt Executor.submit() in addition to Executor.execute() method inward Java? is i of the good multi-threading questions for experienced Java programmers, to a greater extent than oftentimes than non asked inward Investment Banks similar Barclays, Deutsche Bank, or Citibank. H5N1 master copy divergence betwixt the submit() and execute() method is that ExecuterService.submit()can render outcome of computation because it has a render type of Future, but execute() method cannot render anything because it's render type is void. The kernel interface inward Java 1.5's Executor framework is the Executor interface which defines the execute(Runnable task) method, whose primary purpose is to split the chore from its execution.
Any chore submitted to Executor tin live on executed past times the same thread, a worker thread from a thread puddle or whatever other thread.
On the other hand, submit() method is defined inward the ExecutorService interface which is a sub-interface of Executor in addition to adds the functionality of terminating the thread pool, along amongst adding submit() method which tin convey a Callable chore in addition to render a outcome of computation.
See Java Concurrency inward Practice for to a greater extent than details on how thread puddle plant in addition to how tasks are allocated betwixt threads depending upon dissimilar types of thread pools.
This was the telephone substitution divergence betwixt the submit() in addition to execute() method of ExecutorService interface, let's come across a duo of to a greater extent than points to reply this query better.
1) Both submit() in addition to execute() methods are used to submit a chore to Executor framework for asynchronous execution.
2) Both submit() in addition to execute() tin convey a Runnable task.
3) You tin access submit() in addition to execute() from the ExecutorService interface because it besides extends the Executor interface which declares the execute() method.
Apart from the fact that submit() method tin render output in addition to execute() cannot, next are other notable differences betwixt these 2 key methods of Executor framework of Java 5.
1) The submit() tin convey both Runnable and Callable task but execute() tin exclusively convey the Runnable task.
2) The submit() method is declared inward ExecutorService interface field execute() method is declared inward the Executor interface.
3) The render type of submit() method is a Future object but render type of execute() method is void.
Btw, Cay S. Horstmann has besides covered this essential topic on proficient exceptional inward his classic book, Core Java Volume 1 - Fundamentals, tenth Edition. You tin refer that mass for farther reading on this topic.
In general, if y'all are doing computational chore e.g. calculating around conduct chances stats, calculating factorial of large numbers or doing around time-consuming computation e which results inward around value thence utilisation the submit() method. It straightaway returns a Future object, which tin live on after queried to acquire the value of computation past times calling get() method.
Remember, get() is a blocking call thence ever telephone telephone the version which accepts a timeout. While y'all tin utilisation the execute() method if y'all simply desire your code to live on run inward parallel past times worker threads of the thread pool.
Here is a overnice summary of key differences betwixt submit() vs execute() methods inward Java:
That's all almost difference betwixt ExecutorService.submit() in addition to Executor.execute() method inward Java. Remember, the key divergence betwixt them is that submit() render a Future but execute() render nothing.
The thread pools created past times Executors shape ever render a reference of ExecutorService, which provides access to both submit() in addition to execute() method equally it besides extend the Executor interface, which is rootage of execute() method.
Use submit() if your doing computation e.g. calculating value of pie, in addition to utilisation execute() if y'all simply desire the code to live on run inward parallel past times worker threads of thread pool.
Further Learning
Multithreading in addition to Parallel Computing inward Java
Java Concurrency inward Practice - The Book
Applying Concurrency in addition to Multi-threading to Common Java Patterns
Java Concurrency inward Practice Course past times Heinz Kabutz
Any chore submitted to Executor tin live on executed past times the same thread, a worker thread from a thread puddle or whatever other thread.
On the other hand, submit() method is defined inward the ExecutorService interface which is a sub-interface of Executor in addition to adds the functionality of terminating the thread pool, along amongst adding submit() method which tin convey a Callable chore in addition to render a outcome of computation.
See Java Concurrency inward Practice for to a greater extent than details on how thread puddle plant in addition to how tasks are allocated betwixt threads depending upon dissimilar types of thread pools.
Executor.execute() vs ExecutorService.submit() method
As I told inward the outset paragraph that key divergence betwixt the execute() in addition to submit() method is that quondam cannot render the outcome but after tin outcome of computation. Before seeing to a greater extent than difference, let's come across around similarities betwixt the execute() in addition to submit() equally well:1) Both submit() in addition to execute() methods are used to submit a chore to Executor framework for asynchronous execution.
2) Both submit() in addition to execute() tin convey a Runnable task.
3) You tin access submit() in addition to execute() from the ExecutorService interface because it besides extends the Executor interface which declares the execute() method.
Apart from the fact that submit() method tin render output in addition to execute() cannot, next are other notable differences betwixt these 2 key methods of Executor framework of Java 5.
1) The submit() tin convey both Runnable and Callable task but execute() tin exclusively convey the Runnable task.
2) The submit() method is declared inward ExecutorService interface field execute() method is declared inward the Executor interface.
3) The render type of submit() method is a Future object but render type of execute() method is void.
Btw, Cay S. Horstmann has besides covered this essential topic on proficient exceptional inward his classic book, Core Java Volume 1 - Fundamentals, tenth Edition. You tin refer that mass for farther reading on this topic.
When to utilisation submit() in addition to execute() method inward Java
Once y'all empathise the divergence betwixt Executor.execute() in addition to ExecutorService.submit() method y'all own got the cognition to gain upward one's heed when to utilisation submit() in addition to when to utilisation the execute() method.In general, if y'all are doing computational chore e.g. calculating around conduct chances stats, calculating factorial of large numbers or doing around time-consuming computation e which results inward around value thence utilisation the submit() method. It straightaway returns a Future object, which tin live on after queried to acquire the value of computation past times calling get() method.
Remember, get() is a blocking call thence ever telephone telephone the version which accepts a timeout. While y'all tin utilisation the execute() method if y'all simply desire your code to live on run inward parallel past times worker threads of the thread pool.
Here is a overnice summary of key differences betwixt submit() vs execute() methods inward Java:
That's all almost difference betwixt ExecutorService.submit() in addition to Executor.execute() method inward Java. Remember, the key divergence betwixt them is that submit() render a Future but execute() render nothing.
The thread pools created past times Executors shape ever render a reference of ExecutorService, which provides access to both submit() in addition to execute() method equally it besides extend the Executor interface, which is rootage of execute() method.
Use submit() if your doing computation e.g. calculating value of pie, in addition to utilisation execute() if y'all simply desire the code to live on run inward parallel past times worker threads of thread pool.
Further Learning
Multithreading in addition to Parallel Computing inward Java
Java Concurrency inward Practice - The Book
Applying Concurrency in addition to Multi-threading to Common Java Patterns
Java Concurrency inward Practice Course past times Heinz Kabutz
0 Response to "Difference Betwixt Executorservice.Submit() As Well As Executor.Execute() Methods Inwards Java?"
Post a Comment