gpt4 book ai didi

java - 理解 java.lang.Thread.State : WAITING (parking)

转载 作者:IT老高 更新时间:2023-10-28 11:45:02 28 4
gpt4 key购买 nike

首先,一个非常愚蠢的问题,我只是想知道等待“ parking ”是什么意思?线程是在等待停放还是刚刚停放并因此处于等待状态?当 parking 发生时,占用了多少 CPU/内存资源?停放线程的目的是什么?

其次,通过查看 java thread API 中的 park 方法

Disables the current thread for thread scheduling purposes unless the permit is available.

If the permit is available then it is consumed and the call returns immediately; otherwise the current thread becomes disabled for thread scheduling purposes and lies dormant until one of three things happens.....

英语不是我的主要语言,所以我很难理解,我打算将“允许”作为一种“允许停放线程”,因此以下问题:

  • 这是什么意思,什么是“许可”,谁以及如何检查这些许可?
  • 这是什么意思:“如果许可证可用,那么它就会被消耗”,它是否被“停放”?
  • 接下来,如果第二点是真的,那么“ parking ”和“hibernate ”有什么区别?如果我有许可证,我可以永久停放它,如果没有,我可以让它“hibernate ”?

谢谢

最佳答案

Permit 表示允许继续执行。 parking 意味着在获得许可之前暂停执行。

不同于 Semaphore的许可,LockSupport 的许可与线程相关联(即,许可被授予特定线程)并且不会累积(即,当线程消耗许可时,每个线程只能有一个许可,它消失了)。

您可以通过调用 unpark() 来授予线程许可。线程可以通过调用 park() 暂停其执行,直到允许可用(或线程被中断,或超时到期等)。当 permit 可用时,停放的线程使用它并退出 park() 方法。

关于java - 理解 java.lang.Thread.State : WAITING (parking),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7497793/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com