Difference between ExecutorService execute() and submit() method in JavaThe ExecutorService.execute() and submit() methods serve the purpose of submitting tasks to an ExecutorService object. The execute() method accepts a Runnable task, while the submit() method accepts both Runnable and Callable tasks. The execute() method does not have a return value, whereas the submit() method returns a Future object. The Future object can be used to get the task result or to check if the task has finished executing. The submit() method is generally more flexible than the execute() method. If you need to get the result of a task or if you need to check if a task has finished executing, then you should use the submit() method. However, if you don't need the result of the task or if you don't need to check if the task has finished executing, then you can use the execute() method. Execute Method:The execute() method is a part of the Executor interface in Java. It is employed to submit a task to an Executor object. The Executor interface is a generic interface that takes a Runnable task as an argument. The execute() method does not return any value. The task can be any object that implements the Runnable interface. The execute() method is a non-blocking method. The "execute()" method does not wait for the task to complete its execution. The execute() method submits the task to the Executor object and then returns. The Executor object will then execute the task at some later time. The execute() method is useful for submitting tasks that do not need to return any value. For example, you could use the execute() method to submit a task that prints a message to the console. Filename: ExecuteExample.java Output: Hello, world! Submit Method:The submit() method is used in Java's ExecutorService interface. The "submit()" method is utilized to submit a task to the ExecutorService for asynchronous execution. When using the submit() method, you can provide a Callable object as input, and it will return a Future object. The Future object can be used to get the task result or to check if the task has finished executing. The submit() method is a very powerful tool for asynchronous execution in Java. The submit() method allows for the submission of any task to the ExecutorService. The submit() method can also be used to get the task result or to check if the task has finished executing. Filename: SubmitExample.java Output: Hello, world! Difference between ExecutorService execute() and submit() method
Next TopicDifference between Java and Core Java |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India