What is the state of the Java thread while waiting and it is not possible to get a lock

A Java stream can be saved either:

  • Unable to get lock.
  • Saving by wait () method.

What is the difference between the two scenarios described above in terms of Java thread state?

Consider the following simple code:

   synchronized(object) {
       object.wait();
       System.out.println("Completed.");
   }

If two methods (for example, ThreadA and ThreadB) are supported by the wait () method. When another thread calls notifyAll (), ThreadA will come to life from waiting and get a lock on the object and continue, for example. ThreadB will also come to life, but will not be able to obtain an object lock, and will be held until ThreadA exits the synchronized block. ThreadB then gets the lock and continues.

As a result, two “Completed” will be printed.

, ThreadB "Being hold by wait()" " ​​, ".

, Java. , .

+4
2

object.wait() object , object.wait() object, , , . wait() .

, , A B (Thread A Thread B) , object, object.notify() object.notifyAll(). notifyAll() JVM, ( A B) object.wait(), synchronized. notify(), JVM A B.

, JVM - , while(notify_condition_for_me), , , object.wait() .

,

synchronized(object) {
   while(myResourceArrived) {//like URL data, JDBC data or something
       object.wait();
   }
   System.out.println("Completed.");
}

A B object.wait(), , object, , synchronized, , object.

+2

All Articles