0

Please refer to the following code of from the Javadoc of Future class:

FutureTask<String> future =
   new FutureTask<String>(new Callable<String>() {
     public String call() {
       return searcher.search(target);
   }});
 executor.execute(future);

P.S: I am not doing any exclusive executor.submit(future) call here .

So I am trying to execute the future task here by calling executor.execute() method. But how is the task getting submitted to the executor framework in the first place? Which line of code above is actually submitting the task to the executor ?

Inquisitive
  • 7,476
  • 14
  • 50
  • 61
  • 1
    The line `executor.execute(future)` is the only **theoretically** possible line since that's the first time you even mention an executor. – Marko Topolnik Jul 10 '12 at 07:59
  • @Bhaskar which line or which word in `execute()` clearly documents that execute also submits ? this is the documentation:"Executes the given command at some time in the future. The command may execute in a new thread, in a pooled thread, or in the calling thread, at the discretion of the Executor implementation." It only says about execution , doesn't say anything about submission. – Inquisitive Jul 10 '12 at 11:42
  • Please read the docs more carefully and make some effort trying to connect things together. There are **all** explained there , and quite clearly - I am not going to argue with you on this. – Bhaskar Jul 10 '12 at 13:12
  • @Bhaskar I do not want to start arguing either but at best it can be said that it was documented implicitly not "quite clearly" per se. – Inquisitive Jul 10 '12 at 13:19

3 Answers3

3

It's the line

executor.execute(future);

The javadoc of this method says:

Executes the given command at some time in the future. The command may execute in a new thread, in a pooled thread, or in the calling thread, at the discretion of the Executor implementation.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • 1
    so execute does a "submit now and execute later" thing ? If yes is there any reason to ever use executor.submit() method ? We never want a task to be submitted and not getting executed ? – Inquisitive Jul 10 '12 at 08:01
  • 1
    The whole point of an executor is to execute tasks. If you don't want a task executed, you should not submit it to an executor. The `submit()` method is interesting because it returns a Future, that you can use to get the result of the submitted Runnable or Callable: it creates the Future for you, instead of forcing you to create one and executing it. But it does the same thing as `execute()`: it submits a tack that will be executed some time. – JB Nizet Jul 10 '12 at 08:07
  • @JBNizet, Does it imply that the submit will internally call `execute()` but after doing some additional work so that it can return `Future` object ? – Santosh Jul 10 '12 at 08:13
  • Yes. Read its source code. It creates a FutureTask wrapping the Runnable or Callable, calls `execute()` with this FutureTask as argument, and returns it. – JB Nizet Jul 10 '12 at 08:18
  • @Santosh: You're right. Have a look at the source code of `AbstractExecutorService`: http://www.docjar.com/html/api/java/util/concurrent/AbstractExecutorService.java.html and scroll to the `submit` method. – Tudor Jul 10 '12 at 08:18
  • @Santosh see the [code snippet in Tudors answer below](http://stackoverflow.com/questions/11408873/what-method-is-submitting-the-task-to-the-executor-framework-in-the-code-below/11409191#11409191). submit() does call execute(). – Inquisitive Jul 10 '12 at 08:18
3

Your base question was answered, I just want to comment on execute vs. submit.

Basically neither of them are guaranteed to execute your code immediately. If the pool is overloaded with tasks, it will have to wait until all the previous tasks in the queue are finished before executing your task.

Now, you can see the difference in method signature:

void execute(Runnable command) 
public Future<?> submit(Runnable task)

Hence, submit allows you to get a Future reference that you can later use to wait for the task to finish or cancel it if you want.

Bonus, to fully clear things up, having a look at the source of AbstractExecutorService, we can see that the implementation of submit is in fact:

103       /**
104        * @throws RejectedExecutionException {@inheritDoc}
105        * @throws NullPointerException       {@inheritDoc}
106        */
107       public Future<?> submit(Runnable task) {
108           if (task == null) throw new NullPointerException();
109           RunnableFuture<Void> ftask = newTaskFor(task, null);
110           execute(ftask);
111           return ftask;
112       }

In line 110 you can see that it actually calls execute, therefore their functionality is identical, modulo the Future part.

Tudor
  • 61,523
  • 12
  • 102
  • 142
0

I am not doing any exclusive executor.submit(future)

Executor interface has one method execute which takes Runnable as parameter.The FutureTask class is an implementation of Future that implements Runnable, and so may be executed by an Executor.

So you are able to run below code

executor.execute(future);

amicngh
  • 7,831
  • 3
  • 35
  • 54