public class Bees {
public static void main(String[] args) {
try {
new Bees().go();
} catch (Exception e) {
System.out.println("thrown to main" + e);
}
}
synchronized void go() throws InterruptedException {
Thread t1 = new Thread();
t1.start();
System.out.print("1 ");
t1.wait(5000);
System.out.print("2 ");
}
}
Output of this Program is :
1 thrown to main
I am not getting why this thrown to main
came over here.
What is the wait () method in Java? The wait () method is defined in Object class which is the super most class in Java. This method tells the calling thread (Current thread) to give up the lock and go to sleep until some other thread enters the same monitor and calls notify () or notifyAll (). It is a final method, so we can’t override it.
So, when wait () method is called by a thread, then it gives up the lock on that resource and goes to sleep until some other thread enters the same monitor and invokes the notify () or notifyAll () method. Calling notify () wakes only one thread and calling notifyAll () wakes up all the threads on the same object.
Exceptions in Java. Default Exception Handling : Whenever inside a method, if an exception has occurred, the method creates an Object known as Exception Object and hands it off to the run-time system(JVM). The exception object contains name and description of the exception, and current state of the program where exception has occurred.
A big difference between sleep () method and wait () method is that sleep () method causes a thread to sleep for a specified amount of time while wait () causes the thread to sleep until notify () and notifyAll () are invoked.
You are getting a java.lang.IllegalMonitorStateException
because the object you are calling wait()
on (t1
) does not own the synchronization lock.
Note that, when you declare a method as synchronized
, the lock owner for that method is the current object (your instance of Bees
in that case). If you want to call wait()
on t1
, you need to synchronize on t1
:
...
Thread t1 = new Thread();
synchronized(t1) {
t1.start();
System.out.print("1 ");
t1.wait(5000);
}
...
On a side note, when catching an exception, you should always include the exception itself to the log output, at least like
...
} catch (Exception e) {
System.out.println("thrown to main" + e);
}
...
Otherwise you might miss important information (such as which exception was actually thrown).
See also The Java™ Tutorials: Synchronization.
You need to call wait
from inside synchronized
block. The current thread should obtain the object's monitor before it waits.
Copied from JavaDoc:
The current thread must own this object's monitor. The thread releases ownership of this monitor and waits until another thread notifies threads waiting on this object's monitor to wake up either through a call to the notify method or the notifyAll method. The thread then waits until it can re-obtain ownership of the monitor and resumes execution.
synchronized (obj) {
while (<condition does not hold>)
obj.wait();
... // Perform action appropriate to condition
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With