Thread.isAlive()与Thread.join() [英] Thread.isAlive() vs Thread.join()

查看:200
本文介绍了Thread.isAlive()与Thread.join()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对isAlive vs join()进行了很多搜索,但是没有得到适当的解释. 假设我具有线程1和线程2,并且仅在线程1完成后才需要启动线程2.

I have googled a lot about isAlive vs join(), but didn't get a proper explanation. Suppose I have Thread-1 and Thread-2 and I need to start Thread-2 only after Thread-1 has completed.

以上两种方法都可以实现.

The above requirement can be achieved using both the methods.

  1. 那有什么区别?
  2. 在哪种情况下使用哪种方法?

推荐答案

.isAlive() just returns a boolean that indicates whether the thread is alive, the status of the thread - it returns true or false, indicating whether the thread is "done".

.join() 使当前线程处于等待状态,直到您正在调用.join()的线程完成为止.例如,如果您需要在另一个线程中进行一些计算,然后再在当前线程中继续处理其余代码.

.join() makes the current thread you are in wait until the thread that you're calling .join() on has completed. For instance, if you have some calculations that needs to be done in another thread before moving on with the rest of your code in your current thread.

我假设它是线程 -class.这是一个好教程以了解多线程.它看起来似乎很繁琐,但是一旦您掌握了线程,您将立即成为更好的程序员.它非常有用.

I'm assuming it's the Thread-class from Java you're referring to. Here's a good tutorial for understanding multithreading. It can seem like heavy stuff, but once you get your head around threads, you'll become a much better programmer instantly. It's incredibly useful.

这篇关于Thread.isAlive()与Thread.join()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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