You get java.lang.IllegalMonitorStateException because the object you call wait() on ( t1 ) does not have a synchronization lock.
Note that when you declare a method as synchronized , the owner of the lock for this method is the current object (your Bees instance in this case). If you want to call wait() on t1 , you need to synchronize with t1 :
... Thread t1 = new Thread(); synchronized(t1) { t1.start(); System.out.print("1 "); t1.wait(5000); } ...
On the side of the note, when you catch an exception, you should always include the exception itself in the output of the log, at least as
... } catch (Exception e) { System.out.println("thrown to main" + e); } ...
Otherwise, you can skip important information (for example , which was excluded).
See also Java ™ Tutorials: Synchronization.
source share