Home > Mobile >  When should I use Executor over ExecutorService
When should I use Executor over ExecutorService

Time:01-02

I am learning multi-threading and I came across this SO post.

  • Executor just executes stuff we give it.

  • ExecutorService adds startup, shutdown, and the ability to wait for and look at the status of jobs we've submitted for execution on top of Executor (which it extends).

I am still trying to wrap my head around the usage of Executor and ExecutorService.

  1. When would be a scenario when I would use Executor over ExecutorService? Can I use ExecutorService everywhere?
  2. Is there a way to add an explicit shutdown for an Executor's execute or what is the general practice around it?

The below code where I use Executor doesn't terminate. Is this the right way to use an Executor or am I missing something here? How can I properly terminate the code? I am aware that Executor doesn't have a shutdown. So is it expected for this program to not terminate?

import java.util.concurrent.*;

public class A {
    public static void main(String[] args) {
        Executor executor = Executors.newFixedThreadPool(3);
        executor.execute(() ->
                System.out.println("Executor Class: "  
                        Thread.currentThread().getId()   ", Name: "  
                        Thread.currentThread().getName()));
    }
}

If I replace the above with ExecutorService and add a shutdown, the program terminates fine.

import java.util.concurrent.*;

public class A {
    public static void main(String[] args) {
        ExecutorService executor = Executors.newFixedThreadPool(3);
        executor.submit(() ->
                System.out.println("ExecutorService Class: "  
                        Thread.currentThread().getId()   ", Name: "  
                        Thread.currentThread().getName()));
        executor.shutdown();
    }
}

CodePudding user response:

Q: When would be a scenario when I would use Executor over ExecutorService?

A: When you know that you are never going to use any of the ExecutorService methods.


Q: Can I use ExecutorService everywhere?

A: Assuming that you instantiated an instance of ExecutorService, yes.


Q: Is there a way to add an explicit shutdown for an Executor's execute or what is the general practice around it?

If you are asking if you can shut down the entire Executor, no there isn't a way.

Executor service is an interface. In the general sense, it could be implemented in a way that can't be shut down at all. But either way, if you don't know how the executor is implemented, then you don't know how to tell it to shut itself down.

(Sure ... if you know that it is likely to be a ExecutorService, then you could type cast it to ExecutorService and then call ExecutorService.shutdown().)

On the other hand, if you are asking if you can cancel a specific task submitted by Executor.execute, the answer is also No.

But if you use the ExecutorService.submit methods, you will get a Future for each task. You may be able to use Future.cancel() to stop the corresponding task from running or interrupt it if it is currently running.

CodePudding user response:

You should use the ExecutorService interface to manage the executor service. This interface would normally be used by the object that creates or owns the executor service.

An object or method that just needs to execute stuff using an executor that is managed elsewhere should take parameters of type Executor. You can pass an instance of ExecutorService, since it implements Executor.

  • Related