kenWay
kenWay

Reputation: 101

sleep() and context switching in java threads

Lets assume a situation like this :

enter image description here

Lets say Thread0 access the lockObject first and then Thread0 going to sleep for 1000ms.

synchronized(lockObject) {
    Thread0.sleep(1000);
}

Thread1 also waiting for access the lockObject.

What happened in these kind of situations? Is that context switching will pause for 1000ms because of the sleep(1000)?

Upvotes: 0

Views: 737

Answers (1)

Vikas
Vikas

Reputation: 7175

When you call Thread0.sleep(1000);, the thread does not release the lock on lockObject. So yes context switching will pause for 1000ms.

Upvotes: 1

Related Questions