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…