Learn to cancel a task submitted to an executor service if the task still has to be executed and/or has not been completed yet. We can use the cancel() method of Future object that allows making the cancellation requests. 1. Future cancel() API The Future.cancel() method takes one argument of type boolean. boolean cancel(boolean mayInterruptIfRunning); Depending on the value of mayInterruptIfRunning and the status of the task submitted…
Learn to use ExecutorService.invokeAny(tasks) method where we execute multiple tasks at the same time, but we make a decision when any one of those tasks is completed and return its result. 1. invokeAny() method This method executes the given list of tasks, returning the result of one that has completed successfully (i.e., without throwing an exception),…
Learn to run multiple Callable tasks with ExecutorService.invokeAll() API and processing all the results returned from tasks in form of Future class instances in this ExecutorService Callable example. 1. ExecutorService invokeAll() API The invokeAll() method executes the given list of Callable tasks, returning a list of Future objects holding their status and results when all are complete. It is an overloaded method and is in two forms. The second method takes extra…
ExecutorService interface provides 3 methods shutdown(), shutdownNow() and awaitTermination() for controlling the termination of tasks submitted to executors. Learn how to use these methods under different requirements. 1. Difference between shutdown(), shutdownNow() and awaitTermination() Let us start with checking out the syntax of these methods. void shutdown(); List<Runnable> shutdownNow(); boolean awaitTermination(long timeout, TimeUnit unit); he shutdown() initiates an orderly shutdown in which previously…
Learn to use Java ExecutorService to execute a Runnable or Callable class in an asynchronous way. Also, learn the various best practices to utilize it in the most efficient manner in any Java application. 1. What is Executor Framework? In simple Java applications, we do not face many challenges while working with a few threads. If we…
1. Java sleep() and wait() – Discussion sleep() is a method which is used to pause the process for few seconds or the time we want to. But in case of wait() method, thread goes in waiting state and it won’t come back automatically until we call the notify() or notifyAll(). The major difference is that wait() releases the lock or monitor while sleep() doesn’t…
yield() method Theoretically, to ‘yield’ means to let go, to give up, to surrender. A yielding thread tells the virtual machine that it’s willing to let other threads be scheduled in its place. This indicates that it’s not doing something too critical. Note that it’s only a hint, though, and not guaranteed to have any effect at…
1. Create Thread using Runnable Interface vs Thread class Let’s quickly check the java code of usage of both techniques. 1.1. Runnable interface Java program to create thread by implementing Runnable interface. public class DemoRunnable implements Runnable { public void run() { //Code } } //start new thread with a “new Thread(new demoRunnable()).start()” call 1.2. Thread class…
In this post, we are going to discuss thread priorities in detail and the different types of thread priorities in Java, and how a thread scheduler executes various threads based on their priorities. We will also see how we can set thread priority of a thread and how we can get the priority of an existing thread. 1. Introduction to…