J2EEOnline J2EE   





Java Questions 11 - 20  «Prev  Next»
Java Questions 12
  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. When notify() is called, does that mean the lock becomes available at that moment?
    Answer:
    No. 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 
    
September 23, 2017