Java Questions 11 - 20  «Prev  Next»

Methods of the Object Class in Java

  1. What is the relationship between wait() and notify()?

    Answer:
    The wait() method causes the current thread to wait until another thread notifies it of a condition change. You use wait() in conjunction with notify() to coordinate the activities of multiple threads using the same resources.

  2. What happens if the thread calling wait() does not own the lock?

    Answer:
    The thread will throw an IllegalMonitorStateException.

  3. What will the following method do?
    a.wait(2000);
    


    Answer:
    The thread releases the lock and waits for notify() only for a maximum of 2 seconds, then goes back to the Runnable state.

  4. In Java SE 17, when the notify() method is called, does that mean the lock becomes available at that moment?

    Answer:
    In Java SE 17, the behavior of the `notify()` method, as part of the object monitoring mechanism, has not changed from its fundamental design in the Java language. When `notify()` is called on an object, it signals one of the threads that is waiting on that object's monitor (which had previously called `wait()` on the same object) to wake up from the wait set. However, this does not mean the lock becomes immediately available at the moment `notify()` is called. The thread that invoked `notify()` still holds the lock until it exits the synchronized block or method, at which point the lock is released. Only after the lock is released can the notified thread begin to compete for the lock with any other threads that might also be waiting to acquire the same lock. If the notified thread acquires the lock (because it happens to be the next thread scheduled by the thread scheduler), it will then move out of the wait set and re-enter the synchronized block or method where it was waiting.
    It is critical to understand that the `notify()` method itself does not release the lock; the lock is only released when the synchronized block or method has completed, or when `wait()`, `join()`, or `sleep()` is invoked within the block, which explicitly relinquishes the lock for the duration of these methods' operations.
    If the thread is still completing the code within a synchronized block, the lock is not released until the thread moves out of the synchronized block.


  5. How is notifyAll() different from notify()?

    Answer:
    You can use notifyAll() on the object to let all the htreads rush out of the waiting area and back to runnable.
    public static final Thread.State RUNNABLE is the thread state for a runnable thread.

  6. If you have many threads waiting on an object, which thread() will notify affect?

    Answer:
    Which one exactly is not specified and depends on the JVM.

  7. What is IllegalThreadStateException?

    Answer:
    public class IllegalThreadStateException extends IllegalArgumentException
    
    and is thrown to indicate that a thread is not in an appropriate state for the requested operation.
    java.lang
    Class IllegalThreadStateException
    
    java.lang.Object
    java.lang.Throwable
    java.lang.Exception
    java.lang.RuntimeException
    java.lang.IllegalArgumentException
    java.lang.IllegalThreadStateException
    All Implemented Interfaces:
    


  8. When using wait(), notify() or notifyAll(), why should you always have a while loop around the wait() method?

    Answer:
    The wait() method checks a condition and forces continued waiting until the condition is met.

  9. Which methods exists in the interface java.lang.Runnable?

    Answer:
    public void run() {}
    

  10. To which class do the methods wait(), notify(), and notifyAll() belong to?

    Answer:
    java.lang.Object 
    

SEMrush Software