reentrantlock in java

The ReentrantLock class implements the Lock interface. It is the most widely used implementation class of Lock interface. It works in same way as synchronized keyword with extended capabilities. As the name suggest, it allows threads to re-enter into lock on a resource more than once. When the thread first enters into lock, a hold … Read more

Inter-thread communication in java

Inter-thread communication: Inter-thread communication is a process in which a thread is paused running in its critical region and another thread is allowed to enter (or lock) in the same critical region to be executed. i.e. synchronized threads communicate with each other. Below object class methods are used for Inter-thread communication process: 1. wait(): this … Read more

Synchronized block in java

Synchronized block: Synchronized block is used to synchronize a critical block of code instead of whole method. Note: Synchronized block is preferred over synchronized method because in case synchronized block only critical block of code is locked not whole method, hence performance will be better in this case. Throw NullPointerException if object passed in synchronized … Read more

Static synchronization in java

Static synchronization is achieved by static synchronized methods. Static synchronized method locked on class and non-static synchronized method locked on current object i.e. static and non-static synchronized methods can run at same time. It can produce inconsistency problem. If static synchronized method is called a class level lock is acquired and then if an object … Read more

Synchronized method in java

Synchronized method: A method declared with synchronized keyword is known as synchronized method. A synchronized method can be static or non-static. Example: multithreading example without synchronization. MultiThreadExample.java /** * This program is used to show the multithreading * example without synchronization. * @author w3spoint */ class PrintTable{ //not-synchronized method. public void printTable(int n){ System.out.println("Table of … Read more

Synchronization in java

Synchronization: Synchronization is a process of controlling mutual exclusive problem in multithreading environment. Only one thread can access a resource at a particular instance of time. When a thread accesses a synchronized block or method, it acquires a lock on it and release the lock either after completion of method/block or any exception occur. Note: … Read more

Starvation in java

Starvation: Starvation is a situation when a thread is in waiting state from long period because it not getting access of shared resources or because higher priority threads are coming. Example: StarvationExample.java /** * This program is used to show the starvation problem. * @author w3spoint */ public class StarvationExample implements Runnable{ private final Object … Read more

Deadlock in java

Deadlock: When n threads are in waiting state in such a way that thread1 is waiting for some resource which is held by thread2 and thread2 is waiting for some resource held by thread3 and so on thread n is waiting for some resources held by thread1, this situation when all threads are in waiting … Read more

Thread.yield() and Thread.sleep() methods

yield() method: Causes the currently executing thread object to temporarily pause for giving a chance to the remaining waiting threads of the same priority to execute. If no such thread than same thread will continue its execution. If multiple threads of same priority are in waiting state then it depends on thread scheduler which thread … Read more

Can we call run method directly?

Yes, we can call run method directly. Only difference is that when start method is called it creates a separate call stack for that thread but in case when run method is called directly from main method it will not create a new call stack. Run method is goes into current call stack. i.e. No … Read more