-
What are the states of an object
Thread
?- NEW
- RUNNABLE
- BLOCKED
- WAITING
- TIMED_WAITING
- TERMINATED
-
What states can a thread go to when entering a block
synchronized
?- RUNNABLE
- BLOCKED
In RUNNABLE, if the block of code marked with
synchronized
is not occupied by another thread. Otherwise, our thread will get the BLOCKED state and wait for the mutex object to be released. - What state will the thread go to when the method is called
wait()
?Calling this method puts the thread into the WAITING state.
The methodwait()
can only be called inside a blocksynchronized
on a mutex object that has been “locked (locked)” by the current thread, otherwise the method will throw an IllegalMonitorStateException .Object monitor = getMonitor(); synchronized(monitor) { … monitor.wait(); … }
When a method is calledwait()
, the current thread releases the lock on themonitor
and enters the WAITING state, waiting for the method to be calledmonitor.notify()
bymonitor.notifyAll()
another thread. As soon as this happens, the thread will wake up and if the monitor was not busy, it will grab it and continue working.
If the monitor is occupied by another thread, the current thread will enter the BLOCKED state. -
What state will the thread go to when the method is called
Calling this method puts the thread into the TIMED_WAITING state.wait(500)
?
By analogy with the methodwait()
,wait(timeout)
it can only be called inside a blocksynchronized
on a mutex object that has been “locked (locked)” by the current thread.Object monitor = getMonitor(); synchronized(monitor) { … monitor.wait(500); … }
When the method is called
wait()
, the current thread releases the lock on the objectmonitor
and sleeps for 500 milliseconds. The objectmonitor
can be captured by another thread.
After 500 milliseconds, the thread will wake up and ifmonitor
it was not busy, it will grab it and continue working.
If the monitor is occupied by another thread, the current thread will enter the BLOCKED state.What state will the thread go to when the method is called
notify()
?Object monitor = getMonitor(); synchronized(monitor) { … monitor.wait(); … }
Aftermonitor.wait()
, the thread will enter the WAITING state. A methodnotify()
called by another thread on the objectmonitor
will transition the thread from the WAITING state to the RUNNABLE state unless the monitor object is captured by another thread, otherwise to the BLOCKED state. -
What state will the thread go to when the method is called
notifyAll()
?notifyAll()
"wake up" all threads. One of all the WAITING threads will enter the RUNNABLE state, grab the monitor of the object being used, and continue its work. The rest will be in the BLOCKED state. As soon as the first "awakened" thread releases the monitor that all the others are waiting for, the next thread will repeat its fate (an arbitrary thread will go from the BLOCKED state to the RUNNABLE state). This will continue until all awakened threads leave the BLOCKED state. -
Three threads in a synchronized block called
Two of them will go to the BLOCKED state, one to the RUNNABLE statewait()
on a mutex object. What state will these threads go to if the fourth thread callsnotifyAll()
? -
How is it different
Despite the fact that andjoin(500)
fromwait(500)
?join(500)
willwait(500)
put the current thread into the TIMED_WAITING state, there are significant differences between them:join(500)
it is called on a thread,wait(500)
it is called inside a synchronized block on the object on which this block is synchronized.
When called,join(500)
the current thread will wait 500 milliseconds for the thread whose methodjoin()
was called to terminate.
When called,wait(500)
the current thread will release the lock on the synchronized object, and sleep for 500 milliseconds.
After 500 milliseconds in both cases, the threads will continue to run. -
How is it different
wait(500)
fromsleep(500)
?sleep(500)
called on a thread,wait(500)
called inside a synchronized block on the object on which this block is synchronized.
When called,sleep(500)
the current thread will wait 500 milliseconds, then continue.
When called,wait(500)
the current thread will release the lock on the synchronized object, and sleep for 500 milliseconds. -
What state will the thread go to when the method is called
When a method is calledyield()
?yield()
, the current thread "skips its turn" and java immediately switches to the execution of the next thread. The thread goes from staterunning
to stateready
. The running & ready states are substates of the RUNNABLE state.
GO TO FULL VERSION