线程由于等待 JVM 处理的对象锁而处于阻塞状态是如何造成的

发布于 2024-10-14 04:27:56 字数 251 浏览 2 评论 0原文

我已经看到线程可以通过不同的方式进入阻塞状态。我有兴趣知道线程处于阻塞状态后到底会发生什么。如何恢复到运行状态?

如果它被 sleep(time) 阻塞,那么它会在 time 毫秒后移动到可运行队列。如果它在 I/O 操作上被阻塞,一旦完成,它就会进入可运行队列。

当它等待对象锁时,它如何进入可运行队列?它如何知道它等待的对象上的锁现在可用?有人还可以解释 I/O 上阻塞线程如何工作的内部原理吗?

如果我对上述任何主题的理解不正确,请纠正我。

I have seen there are different ways a thread could get to blocked state. I'm interested to know what exactly happens after a thread is in blocked state. How does it get back to running state?

If its blocked by sleep(time) then it moves to the runnable queue after time milli secs. If its blocked on a I/O operation it gets into the runnable queue once that is done.

How does it get to the runnable queue when it is waiting on an objects lock? How does it know that the lock on the object its waiting for is now available? Can some one also explain the internals of how the blocked thread on I/O works?

Please correct me if my understanding on any of the above topics isn't right..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爱*していゐ 2024-10-21 04:27:56

当它等待对象锁时,它如何到达可运行队列?

如果线程由于尝试进入synchronized块而被阻塞,则该线程会被自动标记当另一个线程(持有锁)通过退出同一对象的同步块来释放锁时,该线程可运行。

如果当前线程由于调用 someObject.wait() 而被阻塞,则当另一个线程调用 someObject.notify() 时,该线程将被“释放”。

在字节码级别上,它看起来如下:

[load some object, obj, onto the operand stack]
monitorenter  // grab the lock

// do stuff

[load obj onto the operand stack again] 
monitorexit   // release the lock

如果其他线程已经持有 obj 的锁,则该线程将挂在 monitorenter 上,直到另一个线程调用 monitorexit代码>.

JLS 没有指定如何实现 monitorentermonitorexit 的具体细节。也就是说,它依赖于 JVM/OS。

有关更多详细信息,请参阅 JLS 等待集和通知

How does it get to the runnable queue when it is waiting on an objects lock?

If the thread is blocked due to trying to enter a synchronized block, the thread is automatically marked as runnable when the other thread (holding the lock) releases the lock by exiting from a synchronized block of the same object.

If the current thread is blocked due to a call to someObject.wait(), the thread is "released" when another thread calls someObject.notify().

On the bytecode level it looks as follows:

[load some object, obj, onto the operand stack]
monitorenter  // grab the lock

// do stuff

[load obj onto the operand stack again] 
monitorexit   // release the lock

If someone else already holds the lock of obj, the thread will hang on monitorenter until the other thread calls monitorexit.

The exact details of how monitorenter and monitorexit should be implement is not specified by the JLS. That is, it is JVM/OS dependent.

For further details, refer to JLS Wait Sets and Notifications.

无语# 2024-10-21 04:27:56

在接近代码的级别上,它看起来像这样:

线程 1:

Object mutex = new Object();
....
synchronized(mutex) {
    //lock to mutex is acquired.
    mutex.wait(); //lock to mutex is released. Thread is waiting for somebody to call notify().
    doSomething();
}

线程 2:

synchronized(Thread1.mutex) {
    //acquires the lock on mutex. 
    //Can be done only after mutex.wait() is called from Thread1
    // and the lock is released
    Thread1.mutex.notify(); // notifies Thread1 that it can be resumed.
}

一般来说,您应该记住 Thread.sleep() 持有资源上的锁,但 Thread.wait() 释放锁并且可以被其他线程通知。

On a close-to-code level it looks like this:

Thread 1:

Object mutex = new Object();
....
synchronized(mutex) {
    //lock to mutex is acquired.
    mutex.wait(); //lock to mutex is released. Thread is waiting for somebody to call notify().
    doSomething();
}

Thread 2:

synchronized(Thread1.mutex) {
    //acquires the lock on mutex. 
    //Can be done only after mutex.wait() is called from Thread1
    // and the lock is released
    Thread1.mutex.notify(); // notifies Thread1 that it can be resumed.
}

In general you should keep in mind that Thread.sleep() holds the lock on the resources, but Thread.wait() releases the locks and can be notified by other Threads.

云柯 2024-10-21 04:27:56

AFAIK JVM 使用本机线程。因此,管理线程调度和上下文切换的是操作系统而不是 JVM。

您可以查看实际的 JVM 源代码。它是开放的。

AFAIK JVM use native threads. So it is OS not JVM that manage thread schedule and context switching.

You can look at actual JVM source code. It's open.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文