javamultithreadingconcurrencycontext-switchingthread-state

Will context-switching happen when thread changes its state?


In java, will context switching happen when thread is changing its state?

I've googled a lot of sites, but did't find any article mentioned about this.

so if anyone can help would be appreciated.

like this:

doSomething();
//when Thread.sleep() is called, thread state will change from runnable to timed_waiting, so will context switching happen?
Thread.sleep(1000);

Solution

  • Yes, sleep() triggers context switching to TIMED_WAITING.

    See: https://docs.oracle.com/javase/8/docs/api/java/lang/Thread.State.html#TIMED_WAITING