-
What are all the states of the 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
synchronized
is not occupied by another thread. Otherwise, our thread will receive the BLOCKED state and will wait for the mutex object to be released. - What state will the thread go to when calling the method
wait()
?Calling this method places the thread in the WAITING state.
The methodwait()
can only be called inside a blocksynchronized
on a mutex object that has been “locked” by the current thread, otherwise the method will throw an IllegalMonitorStateException exception .Object monitor = getMonitor(); synchronized(monitor) { … monitor.wait(); … }
When a method is calledwait()
, the current thread releases the lock from the objectmonitor
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 go into the BLOCKED state. -
What state will the thread go to when calling the method
Calling this method places the thread in 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 calling the method
wait()
, the current thread releases the lock from the objectmonitor
and goes to sleep for 500 milliseconds. The objectmonitor
may 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 go into the BLOCKED state.What state will the thread go to when calling the method
notify()
?Object monitor = getMonitor(); synchronized(monitor) { … monitor.wait(); … }
Aftermonitor.wait()
, the thread will go into the WAITING state. A methodnotify()
called by another thread on an objectmonitor
will move 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 calling the method
notifyAll()
?notifyAll()
"will stay" all the threads. One of all "sleeping" (WAITING) threads will go into the RUNNABLE state, take over the monitor of the object being used and continue its work. The rest will be in the BLOCKED state. As soon as the first “waking up” thread releases the monitor, which everyone else is waiting for, its fate will be repeated by the next thread (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 the synchronized block called
Two of them will go into the BLOCKED state, one into the RUNNABLE statewait()
the 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)
transfer the current thread to the TIMED_WAITING state, there are significant differences between them:join(500)
called on a thread,wait(500)
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 complete.
When called,wait(500)
the current thread will release the lock from the synchronized object and go to sleep for 500 milliseconds.
After 500 milliseconds, in both cases the threads will continue to work. -
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 its work.
When called,wait(500)
the current thread will release the lock from the synchronized object and go to sleep for 500 milliseconds. -
What state will the thread go to when calling the method
When a method is calledyield()
?yield()
, the current thread “skips its turn” and java immediately switches to executing the next thread. The thread goes from staterunning
to stateready
. The running & ready states are substates of the RUNNABLE state.
zor07
Level 31
Level 25. Answers to interview questions on the level topic
Comments
- Popular
- New
- Old
You must be signed in to leave a comment
This page doesn't have any comments yet