简体   繁体   English

为什么thread.join通常用于在android中停止线程

[英]why is thread.join commonly used to stop threads in android

looking at several android examples i see a trend where threads are paused using join() method: 查看几个android示例,我看到一种趋势,其中使用join()方法暂停了线程:

EDIT: please assume that the 2 threads are properly defined. 编辑:请假定正确定义了2个线程。 edited the calls to join() 编辑了对join()的调用

public class MainUI extends Activity{
   private GraphicsViewThread g;
   private BackendDataViewThread b;

   void onCreate(){
      g = new GraphicsViewThread();
      g.start();
      b = new BackendDataViewThread();
      b.start();
   }
   .
   .
   .
   void pauseGame(){
      try {
        g.join();
      }catch (InterruptedException e){
         e.printStackTrace();
      }
      try {
        b.join();
      }catch (InterruptedException e){
         e.printStackTrace();
      }
      GraphicsViewThread = null;
      BackendDataViewThread  = null;
   }
}
  1. which thread does GraphicsViewThread and BackendDataViewThread join to? GraphicsViewThread和BackendDataViewThread加入哪个线程?
  2. what is the need for using join()? 使用join()有什么需要? can i not set the references immediately to null? 我不能立即将引用设置为null吗?

I suspect your code doesn't compile, but regardless - I'll answer. 我怀疑您的代码无法编译,但是无论如何-我会回答。
This is not just about Android, but plain Java. 这不仅涉及Android,还涉及纯Java。
Using "join" ensures that you code will wait for the thread to end. 使用“ join”可确保您的代码将等待线程结束。
Isn't this exactly what you want? 这不是您想要的吗?
In addition, don't set references of thread to null. 此外,请勿将线程的引用设置为null。
You cannot guarantee exactly when a thread ends, so it will be bad practice to set the thread object to null, prior to its ending. 您无法保证确切的线程结束时间,因此在线程对象结束之前将线程对象设置为null是不好的做法。

It's a rendezvous function, which means that the thread you are referencing (GraphicsViewThread for example) will join the parent/main/UI thread at that point (which ever gets there first). 这是一个集合点函数,这意味着您正在引用的线程(例如,GraphicsViewThread)将在那时连接父/主/ UI线程(先到达那里)。 That's the thread that is actually drawing the UI. 那就是实际绘制UI的线程。 You can't just set the reference to null, because it's just a reference. 您不能仅将引用设置为null,因为它只是一个引用。 The task will still be working in the background. 该任务仍将在后台运行。

As far as I've worked with Android, and analyzed it, I've learned just to avoid Threads. 就我使用Android并对其进行分析而言,我了解到只是为了避免使用Threads。 Use, AsyncTask or some other similar container. 使用AsyncTask或其他类似的容器。 It's much more managable. 它更易于管理。

Edit: I've just noticed, as the others mentioned, your code is not correct, it probably will not work. 编辑:我刚刚注意到,正如其他人提到的那样,您的代码不正确,可能无法正常工作。

You have several things wrong in the code and are confused about the purpose of Thread.join . 您在代码中有几处错误,并对Thread.join的用途感到困惑。 The join method has to be called on an instance of a Thread . 必须在Thread的实例上调用join方法。 So you'll want to call join on g and b . 因此,您需要在gb上调用join。 Before you call join, you normally would have to notify those threads in some way that you'd like them to stop. 在调用加入之前,通常必须以希望它们停止的某种方式通知那些线程。 As zaske mentions, join will wait until that thread is done before continuing execution in the thread invoking pauseGame . 就像zaske提到的那样, join将等待该线程完成,然后在线程中继续执行并调用pauseGame

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM