我怎么知道某个线程完成了?

编程入门 行业动态 更新时间:2024-10-28 16:22:54
本文介绍了我怎么知道某个线程完成了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有3个线程连接到服务器.. 我怎么知道第一个线程是否完成? 这样我才能开始第二个线程。

i have 3 threads connecting to server.. how will i know if 1st thread is done? so that i can start 2nd thread.

推荐答案

您创建某种 syncRoot 对象,将其传递给线程1和线程2.线程2等待 syncRoot ( syncRoot.wait()),当线程1完成时,它会通知 syncRoot ( syncRoot.notify ())。 如果线程2的逻辑总是在线程结束时运行,那么为什么你想要这个更有意思1,那么最好不要将它们放在单独的线程上,而只是使用一个线程。线程的全部意义在于它们并行运行,而不是按顺序运行。 是的,有时需要在线程之间同步执行,但如果计划首先运行线程1完成然后运行线程2完成,那么我认为你是在滥用你的线程。 / Fredrik You create some sort of syncRoot object, pass it to both thread 1 and thread 2. Thread 2 waits on the syncRoot (syncRoot.wait()), when thread 1 is done, it notifies on the syncRoot (syncRoot.notify()). What's more interesting is why you want this, if the logic of thread 2 is always running at the end of thread 1, then it's better to not have them on separate threads but just use one thread instead. The whole point of threads in that they run in parallel, not in sequence. Yes, there is a need to sometimes synchronize execution between threads but if the plan is to first run thread 1 to completion and then run thread 2 to completion then I think you're misusing your threads. /Fredrik

更多推荐

我怎么知道某个线程完成了?

本文发布于:2023-08-02 13:24:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1278158.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:线程   完成了   我怎么

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!