0

Executor framework maintains its own pool of Workers which are noting but threads. Then, why we have to pass Thread/Runnable as parameter. Why there is no simple Task interface?

ExecutorService executorService = Executors.newFixedThreadPool(3); // 3 worker thread
executorService.execute(new Thread()); // why thread? Nobody is going to use this as thread?

I am asking this because ThreadPoolExecutor internally uses run method of passed Threads.

Please refer below code excerpt from ThreadPoolExecutor:

final void runWorker(Worker w) {
        Runnable task = w.firstTask;
        w.firstTask = null;
        boolean completedAbruptly = true;
        try {
            while (task != null || (task = getTask()) != null) {
                w.lock();
                clearInterruptsForTaskRun();
                try {
                    beforeExecute(w.thread, task);
                    Throwable thrown = null;
                    try {
                        task.run();
                    } catch (RuntimeException x) {
                        thrown = x; throw x;
                    } catch (Error x) {
                        thrown = x; throw x;
                    } catch (Throwable x) {
                        thrown = x; throw new Error(x);
                    } finally {
                        afterExecute(task, thrown);
                    }
                } finally {
                    task = null;
                    w.completedTasks++;
                    w.unlock();
                }
            }
            completedAbruptly = false;
        } finally {
            processWorkerExit(w, completedAbruptly);
        }
    }

Please let me know if I am missing anything important here.

Atul
  • 1,694
  • 4
  • 21
  • 30

2 Answers2

2

Executor#execute(Runnable) accepts any Runnable interface and Thread also implements Runnable hence it's valid parameter for execute() method.

What Oracle documentation says about The Executor Interface?

The Executor interface provides a single method, execute, designed to be a drop-in replacement for a common thread-creation idiom. If r is a Runnable object, and e is an Executor object you can replace

(new Thread(r)).start();

with

e.execute(r);

In your case internally it becomes:

(new Thread(new Thread())).start();

Read more...

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
1

A Runnable isn't a thread. It's just an interface which defines a run method. Calling the run method of an implementing class doesn't run it in a new thread, it just calls it in the thread you're already in, like with any method call. Essentially, Runnable is exactly the Task interface you suggest.

gandaliter
  • 9,863
  • 1
  • 16
  • 23
  • Well Gandaliter it is not necessary that Executor always process task with the caller thread. Check below snippet from Oracle docs ------------------------"More typically, tasks are executed in some thread other than the caller's thread. The executor below spawns a new thread for each task." class ThreadPerTaskExecutor implements Executor { public void execute(Runnable r) { new Thread(r).start(); } } – user001 Oct 08 '16 at 05:12
  • Yes, the whole point of these interfaces is that they deal with the threading for you, so you can just use simple Runnables, which don't know anything about threading themselves. Of course the Executor runs them in different threads (in general). Sorry if this wasn't clear. – gandaliter Oct 11 '16 at 14:05