Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Submitting FutureTasks to an Executor - why does it work?

Tags:

java

oop

I have the following test code.

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

class  MyTask extends FutureTask<String>{
    @Override
    protected void done() {
        System.out.println("Done");
    }

    public MyTask(Runnable runnable) {
        super(runnable,null);
    }
}

public class FutureTaskTest {

    public static void main(String[] args)  {
        ExecutorService executor = Executors.newSingleThreadExecutor();
        FutureTask<String> future = new MyTask(new Runnable() {
            public void run() {
                System.out.println("Running");
            }
        });

        executor.submit(future);

        try {
            future.get();
        } catch (Exception ex ) {
            ex.printStackTrace();
        }
        executor.shutdownNow();

    }
}

This works fine - the overridden 'done' methond in MyTask is called when the task is done. But how does the executor know how to call that ?

The executor only have these submit methods:

public <T> Future<T> submit(Callable<T> task);
public Future<?> submit(Runnable task);

Internally it seems 'submit' wraps the callable/runnable in a new FutureTask(). As far as the executor is concerned I've submitted a Runnable or Callable - from what I gather from these 2 signatures. How does it know I submitted a FutureTask and know how to call my overridden done() ?

like image 775
leeeroy Avatar asked Nov 10 '09 19:11

leeeroy


People also ask

What does executor submit () do in Java?

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.

What is future in executor service?

When one submit a task to ExecutorService which is take a long running time, then it returns a Future object immediately. This Future object can be used for task completion and getting result of computation.

What does future get do?

If the task is complete, it will return true; otherwise, it returns false. The method that returns the actual result from the calculation is Future. get(). We can see that this method blocks the execution until the task is complete.

What's the difference between future and FutureTask in Java?

Future is a base interface and defines the abstraction of an object which promises results to be available in the future while FutureTask is an implementation of the Future interface.


1 Answers

From the executor's point of view, you've submitted a Runnable task. The run method of this task (implemented by FutureTask) is what calls done at the appropriate time. The executor doesn't make any direct call to done.

like image 193
erickson Avatar answered Nov 15 '22 06:11

erickson