[sleep()和Java线程中的上下文切换

问题描述 投票:0回答:1

让我们假设这样的情况:

enter image description here

假设Thread0首先访问lockObject,然后访问Thread0睡眠1000ms。

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

[Thread1也正在等待访问lockObject

在这种情况下发生了什么?是否由于sleep(1000)而使上下文切换暂停1000ms?]

java multithreading synchronized java-threads thread-synchronization
1个回答
1
投票

当您调用Thread0.sleep(1000);时,线程不会释放对lockObject的锁定。因此,是的,上下文切换将暂停1000ms。

© www.soinside.com 2019 - 2024. All rights reserved.