At some point in my application, I want to put my main thread (ie., the currently executing thread) to sleep for some time or until the background has finished (and wakes it up), whichever comes first.
Here is what I did (which I thought would work, but didn't)
public static void main(String args[])
{
// .... other stuff ...
// show a splash screen
// just think of this as an image
showPlashScreen():
new Thread(new Runnable()
{
public void run()
{
// do some work here
// notify everyone after the work is done
Thread.currentThread().notifyAll();
}
}).start();
// now make the current thread to wait for the background
// or for at most 1000
Thread.currentThread().wait(1000);
disposeSplashScreen();
// ... other stuff ....
}
Executing this, I keep getting java.lang.IllegalMonitorStateException
(Partial) stacktrace:
Caused by: java.lang.IllegalMonitorStateException
at java.lang.Object.wait(Native Method)
.... <cut> ....
Exception in thread "Thread-7" java.lang.IllegalMonitorStateException
at java.lang.Object.notifyAll(Native Method)