brain storm brain storm - 1 year ago 147
Java Question

difference between executor.submit and executor.execute in this code in Java?

I am learning to use

to pool
and send out tasks. I have a simple program below

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

class Processor implements Runnable {

private int id;

public Processor(int id) { = id;

public void run() {
System.out.println("Starting: " + id);

try {
} catch (InterruptedException e) {
System.out.println("sorry, being interupted, good bye!");
System.out.println("Interrupted "+Thread.currentThread().getName());

System.out.println("Completed: " + id);

public class ExecutorExample {

public static void main(String[] args) {
Boolean isCompleted=false;

ExecutorService executor = Executors.newFixedThreadPool(2);

for(int i=0; i<5; i++) {
executor.execute(new Processor(i));

//executor does not accept any more tasks but the submitted tasks continue

System.out.println("All tasks submitted.");

try {
//wait for the exectutor to terminate normally, which will return true
//if timeout happens, returns false, but this does NOT interrupt the threads
isCompleted=executor.awaitTermination(100, TimeUnit.SECONDS);
//this will interrupt thread it manages. catch the interrupted exception in the threads
//If not, threads will run forever and executor will never be able to shutdown.
} catch (InterruptedException e) {

if (isCompleted){
System.out.println("All tasks completed.");
else {
System.out.println("Timeout "+Thread.currentThread().getName());

It does nothing fancy, but creates two
and submits 5 tasks in total. After each
completes its task, it takes the next one,
In the code above, I use
. I also changed to
. But I do not see any difference in the output. In what way are the
submit and execute
methods different?
This what the

Method submit extends base method Executor.execute(java.lang.Runnable) by creating and returning a Future that can be used to cancel execution and/or wait for completion. Methods invokeAny and invokeAll perform the most commonly useful forms of bulk execution, executing a collection of tasks and then waiting for at least one, or all, to complete. (Class ExecutorCompletionService can be used to write customized variants of these methods.)

But its not clear to me as what it exactly means?

Answer Source

As you see from the JavaDoc execute(Runnable) does not return anything.

However, submit(Callable<T>) returns a Future object which allows a way for you to programatically cancel the running thread later as well as get the T that is returned when the Callable completes. See JavaDoc of Future for more details

Future<?> future = executor.submit(longRunningJob);
//long running job is taking too long

Moreover, if future.get() == null and doesn't throw any exception then Runnable executed successfully

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download