user1467855
user1467855

Reputation: 3993

Shutdown executor without executorService in Java

Is there a way to shutdown an Executor in Java without first casting it to ExecutorService? Basically i have a class called Exx that implements Executor. I want to shutdown Exx. How would I do that? Essentially, I need to know how to shutdown an Executor.

EDIT: Example:

class Exx implements Executor{
    @Override
    public void execute(Runnable r) {
        new Thread(r).start();
    }       
}

Upvotes: 0

Views: 210

Answers (1)

Tim Bender
Tim Bender

Reputation: 20442

An Executor by nature of the specific interface is not something that needs to be shutdown. For instance, here is the most basic implementation of Executor.

class NearlyPointlessExecutor implements Executor {
    public void execute(Runnable r) {
        r.run();
    }
}

Clearly in the code above, there is nothing so complicated that anything need be shutdown, yet the provided class adheres completely to the Executor interface.

If your implementation does need to be shutdown, then your options are either to make your own interface or implement ExecutorService.

Update for question edit:

In the case of the provided code, it would not be possible to shutdown the Threads being created because no reference to them is kept in a collection.

However, there is a solution, the implementation provided is essentially the same as the one provided by using: Executors.newCachedThreadPool. Discard your own implementation and use this one instead.

Upvotes: 3

Related Questions