为什么 java.lang.Thread.join() 方法如此命名?

发布于 2024-07-25 14:21:40 字数 194 浏览 6 评论 0原文

有谁知道为什么 java.lang.Thread 的 join() 成员方法如此命名? 它的javadoc是:

等待该线程结束。

当在某个线程上调用 join 时,调用线程正在等待另一个线程死亡并继续执行。 据说调用线程也会死掉,但仍然不清楚作者为什么使用这个名字。

Does anybody know why the method join() member of a java.lang.Thread was named like that? Its javadoc is:

Waits for this thread to die.

When join is called on some thread calling thread is waiting for the other to die and continue execution. Supposedly calling thread will die as well, but still it's not clear why the author used this name.

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

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

发布评论

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

评论(6

留蓝 2024-08-01 14:21:40

这是线程中的一个常见名称——Java 并不是第一个使用它的。 (例如,这也是 pthreads 使用的。)

我想你可以想象它像两个人散步 - 你加入另一个人并与他们一起散步,直到完成,然后再回到你正在做的事情。 这种类比可能就是最初的原因,尽管我同意这并不完全直观。

It's a common name in threading - it's not like Java was the first to use it. (For example, that's what pthreads uses too.)

I guess you could imagine it like two people taking a walk - you join the other one and walk with them until you've finished, before going back to what you were doing. That sort of analogy may have been the original reason, although I agree it's not exactly intuitive.

硪扪都還晓 2024-08-01 14:21:40

之所以这样命名,是因为您基本上是在声明调用执行线程将等待加入给定的执行状态。 它在 posix 和许多其他线程包中也被命名为 join。

在对 join 的调用返回之后(除非它被中断),两个执行线程基本上从那时起一起运行(该线程获取现在终止的线程的返回值)。

It's named this way because you're basically stating that the calling thread of execution is going to wait to join the given state of execution. It's also named join in posix and many other threading packages.

After that call to join returns (unless it was interrupted), the two threads of execution are basically running together from that point (with that thread getting the return value of the now-terminated thread).

明天过后 2024-08-01 14:21:40

这源于当控制流分成并发线程时的并发软件建模。 稍后,两个执行线程会再次加入。

另外 waitToDie() 可能 a) 太长 b) 太病态。

This stems from concurrent software modeling when the flow of control splits into to concurrent threads. Later, the two threads of execution will join again.

Also waitToDie() was probably a) too long and b) too morbid.

魂牵梦绕锁你心扉 2024-08-01 14:21:40

嗯……这并不完全正确,但我想到了一个“等候室”(它实际上不是一个具有特定调度的队列,如 FIFO、HRRN 等)。
当一个线程无法继续运行并且需要等待其他线程完成时,它只需加入等待室中的人员(又名线程)即可开始活动......

well... this isnt really correct but I thought of an "waiting room" (it actually isnt a queue with a certain scheduling as FIFO, HRRN or such).
when a thread cannot go on and needs to wait on some other thread to finish it just joins the guys (aka threads) in the waiting room to get active next...

活泼老夫 2024-08-01 14:21:40

因为您正在等待另一个执行线程(即您正在调用 join 的线程)加入(即死亡)到当前(即调用)线程。

调用线程不会死亡:它只是等待另一个线程这样做。

Because you are waiting for another thread of execution (i.e. the one you're calling join on) to join (i.e. die) to the current (i.e. the calling) thread.

The calling thread does not die: it simply waits for the other thread to do so.

清音悠歌 2024-08-01 14:21:40

这是一个广泛使用的术语(也在 Java 之外)。 我将其视为以某种方式将一个线程与另一个线程关联起来。 我认为 Thread.Associate() 可能是一个更好的选择,但 Join() 也不错。

This is a terminology that is widely used(outside Java as well). I take it as sort of Associating a Thread with another one in some way. I think Thread.Associate() could have been a better option but Join() isn't bad either.

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