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…
This tutorial shows you how to replace multiple consecutive characters with single character. Consider we have an input as: String str = “%%New%%%%”; Here, there are multiple consecutive occurrence of % character and we can replace its each set of consecutive occurrence as shown below: str = str.replaceAll(“(.)\\1+”,”$1″); So, after performing the above replace operation,…
Autoboxing is an automatic conversion of a primitive type into its corresponding wrapper class object by the java compiler. For an example, converting an int to an Integer. Consider the following example: int i = 10; Integer j = i; Here, Integer j expects wrapper class of Integer object, but even though if we assign…
It is an algorithm that searches for an element one by one in an array, until the target element is found. Consider if X is an element to be searched in an array, then linear search performs the following: Compare X with each element of array one by one If X matches with an element,…