Skip to content
Advertisement

Tag: wait

Java Thread is blocked in join()

I have the following simple code in which I put and take from a Queue represented as an ArrayList. I don’t understand why my thread is blocked in join() method. From main I call as follow: Answer Without running it… The close() method holds lock at the time it calls thread.join() and waits on thread (forever) thread is waiting to

How do I make a delay in Java?

I am trying to do something in Java and I need something to wait / delay for an amount of seconds in a while loop. I want to build a step sequencer. How do I make a delay in Java? Answer If you want to pause then use java.util.concurrent.TimeUnit: To sleep for one second or To sleep for a minute.

Java Delay/Wait

How do I delay a while loop to 1 second intervals without slowing down the entire code / computer it’s running on to the one second delay (just the one little loop). Answer

Why must wait() always be in synchronized block

We all know that in order to invoke Object.wait(), this call must be placed in synchronized block, otherwise an IllegalMonitorStateException is thrown. But what’s the reason for making this restriction? I know that wait() releases the monitor, but why do we need to explicitly acquire the monitor by making particular block synchronized and then release the monitor by calling wait()?

Advertisement