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…
In java, join() methods allows one thread to wait for the complete execution of other thread. Consider there are two threads T1 & T2, currently thread T1 is executing, and we invoked join() method on T2 then execution of thread T1 is paused and waits for the completion of execution of Thread T2 and then…
There is no official method to kill a thread in Java. Stopping a thread is entirely managed by the JVM. Although Java provides several ways to manage the thread lifecycle such as a start(), sleep(), stop() (deprecated in Java 1.1), etc. but does not provide any method to kill a thread and free the resources cleanly. Oracle specified the reason for deprecating the…
Thread in Java Table Of Contents Getting Thread Name Setting Name to Thread Using Thread Constructor Using Thread.setName() 1. Getting Thread Name By default, the Java compiler sets a default name of each thread while creating, and we can get the thread name by using the Thread.currentThread().getName() method. In the following example, we created a Thread by implementing the Runnable interface and…
Defining thread safety is surprisingly tricky. A quick Google search turns up numerous “definitions” like these: Thread-safe code is code that will work even if many Threads are executing it simultaneously. A piece of code is thread-safe if it only manipulates shared data structures in a manner that guarantees safe execution by multiple threads at the same…
Executor RejectedExecutionHandler 1. When tasks get rejected Remember, when we finish the execution of an executor, we use the shutdown() method. The executor waits for the completion of tasks that are either running or waiting for their execution. Then, it shuts down the executor. If we send a task to an executor between invoking the shutdown() method and the end…
1. ConcurrentHashMap class The ConcurrentHashMap is very similar to the HashMap class, except that ConcurrentHashMap offers internally maintained concurrency. It means we do not need to have synchronized blocks when accessing its key-value pairs in a multithreaded application. //Initialize ConcurrentHashMap instance ConcurrentHashMap<String, Integer> m = new ConcurrentHashMap<>(); //Print all values stored in ConcurrentHashMap instance for each (Entry<String, Integer> e : m.entrySet()) { system.out.println(e.getKey()+”=”+e.getValue()); } The…
1. Deadlock In Java, a deadlock is a situation where minimum two threads are holding the lock on some different resource, and both are waiting for the other’s resource to complete its task. And, none is able to leave the lock on the resource it is holding. ResolveDeadLockTest.java package thread; public class ResolveDeadLockTest { public static…