Java EE 7 Application Developer Exam: ManagedExecutorService Lifecycle Events

Which interface should you implement if you want to be alerted to the lifecycle events surrounding your task being executed by a ManagedExecutorService?

Question

Which interface should you implement if you want to be alerted to the lifecycle events surrounding your task being executed by a ManagedExecutorService?

Answers

Explanations

Click on the arrows to vote for the correct answer

A. B. C. D.

D.

https://github.com/javaee/concurrency-ee-spec/blob/master/api/src/main/java/javax/enterprise/concurrent/ManagedExecutorService.java

If you want to be alerted to the lifecycle events surrounding your task being executed by a ManagedExecutorService, you should implement the ManagedTaskListener interface.

The ManagedExecutorService is a container-managed thread pool executor that allows you to submit tasks for execution. The lifecycle events surrounding the execution of a task in the ManagedExecutorService include the task being submitted, starting, completing, failing, and canceling.

The ManagedTaskListener interface is used to receive notifications of these lifecycle events. It defines five methods that correspond to the lifecycle events: taskSubmitted, taskAborted, taskDone, taskStarting, and taskFailed.

By implementing the ManagedTaskListener interface and registering your implementation with the ManagedExecutorService, you can receive notifications of these events and take appropriate action based on the event. For example, you might use the taskStarting method to log the start time of the task, and the taskDone method to log the completion time and result of the task.

The Runnable interface is a basic interface in Java that represents a task that can be executed. However, it does not provide any lifecycle event notifications. The ManagedExecutorTask and TaskEventListener interfaces are not part of the Java EE 7 specification and do not exist.