Why are there three “blocking” states in Java, but only one in C #?

In Java, there are three different states of thread blocking: BLOCKED, WAITING, and TIMED_WAITING. (manual)

In C #, there is only one “blocking” state: WaitSleepJoin. (manual)

Why? I can only guess that the different Java and C # implementations are different. I really see no practical reason why there should be three. Whether there is a? (I first studied C # locking, Monitor.pulseAll () etc., and spent half a gnarly hour of debugging with Java today because I assumed that the synchronized Java block, Object # notifyAll (), etc. I I know how they behave differently now, but I don’t understand WHY.)

+5
source share
1 answer

In java, we use WAITING and TIMED_WAITING for a synchronized object. If the thread is in WAITING state, another thread should wake it using notify()

TIMED_WAITING is the same as WAITING, but it will continue automatically when the specified time parameter is exceeded.

A thread is in the BLOCKED state when the thread wants to execute, but it cannot work due to another thread that is running on the same synchronized object.

Thus, we can see that TIMED_WAITING is the same as WAITING, but will stop after a certain time.

But why did java separate BLOCKED and WAITING? This is due to the fact that they are different, as indicated above. BLOCKED means that the thread is running, but it cannot work, due to the start of another thread. And the WAITING state is just waiting for a call to notify()

Why C # has only one state is just a design decision. All java methods show that the thread is not in runnable state, and C # just decided to combine these three states in one variant: WaitSleepJoin

+2
source

Source: https://habr.com/ru/post/1263389/


All Articles