ScheduledExecutorService

For Java 11 Certification Exam Practice Questions, refer http://talks.skilltoz.com/java-11-certification-exam-questions/

In this article, let us discuss the ScheduledExecutorService in Java, from the Java 11 certification perspective.

What is ScheduledExecutorService?

The java.util.concurrent.ScheduledExecutorService is an implementation of ExecutorService that can be used to schedule tasks to run after a delay, or to execute repeatedly with a fixed time interval between each execution. A worker thread from a thread pool executes the tasks asynchronously.

Here is an example to show how it works.

ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
ScheduledFuture scheduledFuture = scheduledExecutorService.schedule(new Callable() 
    {
	    public Object call() throws Exception {
	    System.out.println("Called!");
		return "Called!";
     }
}, 15, TimeUnit.SECONDS);

In the above code, the message “Called!” is printed after a delay of 15 seconds.

References

For more information on this topic, you can refer https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/concurrent/ScheduledExecutorService.html

Leave a Reply

Your email address will not be published. Required fields are marked *