gpt4 book ai didi

java - 从Java语言规范理解等待和中断

转载 作者:行者123 更新时间:2023-12-01 17:41:52 25 4
gpt4 key购买 nike

我试图从 Java 语言规范 chapter 17 中了解线程和锁。它说:

(引用1)

Let t be the thread invoking u.interrupt, for some thread u, where t and u may be the same. This action causes u's interruption status to be set to true.

我相信这对应于“如果前面的条件都不成立,则将设置该线程的中断状态”这句话。来自Thread.interrupt()doc .

问题1。我说得对吗?

它进一步说:

(引用2)

Additionally, if there exists some object m whose wait set contains u, then u is removed from m's wait set. This enables u to resume in a wait action, in which case this wait will, after re-locking m's monitor, throw InterruptedException.

我相信这对应于“如果该线程在调用 wait()wait(long)wait( long, int) Object 类的方法,或 join()join(long) 的方法join(long, int)sleep(long)sleep(long, int),该类的方法,则其中断状态将被清除并且它将收到一个 InterruptedException。”来自Thread.interrupt()doc .

问题2。我说得对吗?
Q3.“使您能够在等待操作中恢复,在这种情况下,在重新锁定m的监视器后,此等待将抛出中断异常

它进一步说:

(引用3)

If a thread is both notified and interrupted while waiting, it may either:

  • return normally from wait, while still having a pending interrupt (in other words, a call to Thread.interrupted would return true)
  • return from wait by throwing an InterruptedException The thread may not reset its interrupt status and return normally from the call to wait

Q4.最后一个陈述是否对应于第一个要点?

(引用4)

Similarly, notifications cannot be lost due to interrupts. Assume that a set s`` of threads is in the wait set of an object m, and another thread performs a notify on m. Then either:

  • at least one thread in s must return normally from wait, or
  • all of the threads in s must exit wait by throwing InterruptedException

Note that if a thread is both interrupted and woken via notify, and that thread returns from wait by throwing an InterruptedException, then some other thread in the wait set must be notified.

Q5.以上最后一句是否对应于第三个引用中的第二个要点?

Q6.第二个要点如何/为何成立,特别是“全部”?

我没有从Thread.interrupt()中得到以下句子背后的逻辑doc :

If this thread is blocked in an invocation of the wait(), wait(long), or wait(long, int) methods of the Object class, or of the join(), join(long), join(long, int), sleep(long), or sleep(long, int), methods of this class, then its interrupt status will be cleared and it will receive an InterruptedException.

Q7.这是否意味着,通过退出等待/ sleep /加入,对interrupt()的 react 或响应已完成,因此清除中断并指示中断及其响应是通过引发 InterruptedException 发生的?

最佳答案

我只想尝试回答您的几个问题之一:

Q3. What does it mean by "enables u to resume in a wait action, in which case this wait will, after re-locking m's monitor, throw InterruptedException"

一点一点:

enables u to resume

正如上面所说:线程 u 在调用 m.wait() 时被阻塞,将停止等待其他线程调用 m.notify( )

...after re-locking m's monitor...

因为如果没有首先重新锁定监视器,m.wait() 必须永远返回。 m.wait() 正常返回还是异常返回并不重要*。原因是,异常的处理程序可能位于 synchronized (m) block 内,并且线程绝不能在不持有监视器的情况下在 synchronized block 内执行。

...throw InterruptedException

再说一次,正如上面所说:m.wait() 调用将抛出异常而不是正常返回。

<小时/>

* 当函数引发异常时,该事件在某些文档上下文中称为“异常返回”。

关于java - 从Java语言规范理解等待和中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60938993/

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