-1

Please don't close this question. It is not related to question What does it mean to "program to an interface"?

I am trying to learn ExecutorService interface and its methods. I created an object of type ExecutorService using Executors class static method newFixedThreadPool() and trying to call the execute() method of ExecutorService interface.

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

class WorkerThread implements Runnable {
    private String message;

    public WorkerThread(String s) {
        this.message = s;
    }

    public void run() {
        System.out.println(Thread.currentThread().getName() + " (Start) message = " + message);

        System.out.println(Thread.currentThread().getName() + " (End)");// prints thread name
    }

}

public class TestThreadPool {
    public static void main(String[] args) {
        ExecutorService executor = Executors.newFixedThreadPool(5);// creating a pool of 5 threads
        for (int i = 0; i < 10; i++) {
            Runnable worker = new WorkerThread("" + i);
            executor.execute(worker);// calling execute method of ExecutorService
        }
        
        executor.shutdown();
        while (!executor.isTerminated()) {
        }

        System.out.println("Finished all threads");
    }
}

In above code, I have created an object of type ExecutorService using Executors class newFixedThreadPool method which creates a pool of 5 threads.

But How executor is calling execute() method here, as it is an abstract method of ExecutorService interface and the Executors class also don't have any execute() method?

Mayank Kumar Thakur
  • 588
  • 1
  • 8
  • 23
  • Hi @RenéLink, I think you have not read my question completely. There is no execute() method in Executors class. You can check in https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Executors.html also. So, how execute() method is implemented here? – Mayank Kumar Thakur Sep 11 '21 at 08:46

2 Answers2

2

But How executor is calling execute() method here, as it is an abstract method of ExecutorService interface and the Executors class also don't have any execute() method?

Executors.newFixedThreadPool returns an instance of a class that implements ExecutorService. This instance has the execute method.

See Executors.newFixedThreadPool(int)

public static ExecutorService newFixedThreadPool(int nThreads) {
    return new ThreadPoolExecutor(nThreads, nThreads,
                                  0L, TimeUnit.MILLISECONDS,
                                  new LinkedBlockingQueue<Runnable>());
}

The ThreadPoolExecutor implments the execute method. See ThreadPoolExecutor.java#L1327. The ThreadPoolExecutor extends AbstractExecutorService and the AbstractExecutorService implements Executor and ExecutorService.

AbstractExecutorService Javadoc

EDIT

Hi, could you also share the link of Executors class all method?

Here is the Executors source

René Link
  • 48,224
  • 13
  • 108
  • 140
0

Indeed there is no method Execute in Executors class. It's a method in the ExecutorService class. You are calling exactly this method. In fact it's a method of the Executor interface, which ExecutorService class implements: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Executor.html#execute(java.lang.Runnable)

Look at the section "All superinterfaces" in the documentation you pasted: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ExecutorService.html

jannis
  • 4,843
  • 1
  • 23
  • 53