Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why three "blocking" thread states in Java but only one in C#? [closed]

In Java, there are three distinct "blocking" thread states: BLOCKED, WAITING, and TIMED_WAITING. (the manual)

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

Why? I can only guess Java's and C#'s respective implementations differ. I don't really see any practical reason why there should be three. Is there any? (I first learned C#'s lock block, Monitor.pulseAll() etc., and spent one and half gnarly hour debugging with Java today, because I assumed Java's synchronized block, Object#notifyAll(), etc. I know how they behave differently now, but I don't understand WHY.)

like image 457
Jahwan Kim Avatar asked Oct 30 '22 13:10

Jahwan Kim


1 Answers

In java we use the WAITING and TIMED_WAITING on a synchronized object. If a thread is being in the WAITING state, another thread has to wake it up 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 BLOCKED state when the thread wants to run, but it can't run due another thread that is running on the same synchronized object.

So as we can see TIMED_WAITING is just the same as WAITING, but will stop waiting after a specified time.

But why did java seperate BLOCKED and WAITING? This is because they are different as mentioned above. BLOCKED means a thread is runnable but it cannot run, due another thread which is running. And the WAITING state is just waiting to be called for a notify()

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

like image 87
gi097 Avatar answered Nov 02 '22 09:11

gi097