我怎么知道某个线程完成了? [英] how will i know that a certain thread is finish?

查看:98
本文介绍了我怎么知道某个线程完成了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有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


这篇关于我怎么知道某个线程完成了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆