The awakened threads will not be able to proceed until the current thread relinquishes the lock on this object in java


We must have seen or hear about the statement “The awakened threads will not be able to proceed until the current thread relinquishes the lock on this object “, let’s discuss this statement in detail.

This statement is generally used when we are working with wait() and notify() methods.


Wait() method causes the current thread to wait until another thread invokes the notify() or notifyAll() method for this object.
Now, as soon as notify() or notifyall() method is called it notifies the waiting thread, but object monitor is not yet available. Object monitor is available only when thread exits synchronized block or synchronized method. So, what happens is code after notify() is also executed and execution is done until we reach end of synchronized block.

In below program, following statement was below notify() and it was executed.
System.out.println("Notify() has been called, Thread-2 have been notified and "
                              + "Thread-1 is about to release object lock");

                

Program >
class MyRunnable1 implements Runnable{
   @Override
   public void run(){
         
          synchronized (this) {
                       try{
                              System.out.println("Thread-1 is sleeping...");
                              Thread.sleep(1000);
                       }catch(InterruptedException e){e.printStackTrace();}
                
                 this.notify(); //It will notify waiting threads, but object lock is yet to be release
                
                 System.out.println("Notify() has been called, Thread-2 have been notified and"
                                  + "Thread-1 is about to release object lock");
                
          }//synchronized block over and the moment block is over,
             //object lock is released & Thread-2 now can acquire object lock.
   }
  
}
class MyRunnable2 extends Thread{
   MyRunnable1 prod;
  
   MyRunnable2(MyRunnable1 obj){
    prod=obj;
   }
  
   public void run(){
          synchronized (this.prod) {
                
                 System.out.println("Thread-2 Waiting..");
                   try{
                       this.prod.wait();
                       }catch(InterruptedException e){e.printStackTrace();}
                
          }
         
          System.out.println("Thread-2 has been notified.");
         
   }
  
}
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class RelinquishingLock {
   public static void main(String args[]) throws InterruptedException{
         
          MyRunnable1 myRunnable1=new MyRunnable1();
          MyRunnable2 myRunnable2=new MyRunnable2(myRunnable1);
         
          Thread thread1=new Thread(myRunnable1,"Thread-1");
          Thread thread2=new Thread(myRunnable2,"Thread-2");
         
          thread2.start();       
          Thread.sleep(100); //This minor delay will ensure that Thread-1 thread starts Thread-2.
          thread1.start();       
         
         
   }
}
/*OUTPUT
Thread-2 Waiting..
Thread-1 is sleeping...
Notify() has been called, Thread-2 have been notified and Thread-1 is about to release object lock
Thread-2 has been notified.
*/




RELATED LINKS>

Thread basics >

What is thread in java

Thread states/ Thread life cycle in java

Difference between Process and Thread in java

Implementing Threads in java by implementing Runnable interface and extending Thread class

Threads implement their own stack - demonstration using program and diagram

Differences between implementing Runnable interface and extending Thread class

Thread behaviour is unpredictable

When threads are not lightweight process in java, in fact they become heavy weight process


run() and Start() methods >

What will happen if we don’t override run method of thread?

What will happen if we override start method of thread?

Difference between starting thread with run() and start() methods in java

Can we start Thread again?

Volatile keyword vs synchronized>

Volatile keyword in java- difference between synchronized and volatile, 10 key points about volatile keyword, why volatile variables are not cached in memory

Differences between synchronized and volatile keyword in detail


Race Condition >

Race condition in multithreading and it's solution



Consumer Producer problem solution using different techniques >
Solve Consumer Producer problem by using wait() and notify() methods in multithreading

solve Consumer Producer problem by using wait() and notify() methods, where consumer can consume only when production is over


How to solve Consumer Producer problem without using wait() and notify() methods, where consumer can consume only when production is over.



BlockingQueue >

Solve Consumer Producer problem by using BlockingQueue in multithreading


Custom implementation of LinkedBlockingQueue class which implements BlockingQueue interface





DeadLock and it’s detection >

Deadlock in multithreading - program to form DeadLock, solving DeadLock, measures to avoid Deadlock.


VisualVM - Thread dumps - Generating and analyzing Thread Dumps using VisualVM - step by step detail to setup VisualVM with screenshots


JSTACK - Thread dumps - Generating and analyzing Thread Dumps using JSATCK - step by step detail to setup JSTACK with screenshots


Reason why suspend() and resume() methods are deprecated and Deadlock prone

destroy() method in java - usage, reason why destroy() method is deprecated and Deadlock prone.




Important Thread methods (salient features, usage with programs)>

Join() method - ensure all threads that started from main must end in order in which they started and also main should end in last. Types of join() method-10 salient features of join

Sleep() method in threads - 10 key features with programs

Yield() method in threads - 8 key features with programs

Wait() and notify() methods- Definition, 8 key features, solving consumer producer problem with & without these methods and consequences of not using wait() and notify() methods.

Daemon threads - 12 salient features of Daemon Thread

2 alternate ways to stop thread, as stop() method is deprecated

Using Suspend and resume method in threads



Differences between important thread methods >

Difference between wait() and sleep() method in threads

Differences and similarities between yield() and sleep() in threads

Difference between notify() and notifyAll() methods, with program

Difference between wait() and wait(long timeout) -What will be Thread states




Object and class lock >

Acquiring object lock - synchronization blocks and methods- multiple threads may exist on same object but only one thread of that object can enter synchronized block/method at a time.
Acquiring lock on class, 2 Ways to acquire lock on class

Difference between object Lock and class Lock



Thread Pool, Thread local, Busy Spin >

Implement Thread pool in java

ThreadLocal in multithreading in java - methods and usage with program

Busy Spin - What is busy spin? Consumer Producer problem with busy spin and solution to busy spin.



Situation based questions -

Suppose you have 2 threads (Thread-1 and Thread-2) on same object. Thread-1 is in synchronized method1(), can Thread-2 enter synchronized method2() at same time?

Suppose you have 2 threads (Thread-1 and Thread-2) on same object. Thread-1 is in static synchronized method1(), can Thread-2 enter static synchronized method2() at same time?

Suppose you have 2 threads (Thread-1 and Thread-2) on same object. Thread-1 is in synchronized method1(), can Thread-2 enter static synchronized method2() at same time?

Suppose you have thread and it is in synchronized method and now can thread enter other synchronized method from that method?

Suppose you have thread and it is in static synchronized method and now can thread enter other static synchronized method from that method?

Suppose you have thread and it is in static synchronized method and now can thread enter other non static synchronized method from that method?

Suppose you have thread and it is in synchronized method and now can thread enter other static synchronized method from that method?

Suppose you have 2 threads (Thread-1 on object1 and Thread-2 on object2). Thread-1 is in synchronized method1(), can Thread-2 enter synchronized method2() at same time?

Suppose you have 2 threads (Thread-1 on object1 and Thread-2 on object2). Thread-1 is in static synchronized method1(), can Thread-2 enter static synchronized method2() at same time?




Guidelines to threadsafe code >



Interviews >

THREADS - Top 75 interview questions and answers (detailed explanation with programs), includes important MultiThreading OUTPUT questions




No comments:

Post a Comment