简体   繁体   English

如何在java中等待线程的启动

[英]How to wait for the start of a thread in java

I have a litte race condition in my current android instrumentation test. 我目前的android仪器测试中有一个小小的竞争条件。 What I want is: 我想要的是:

  1. T1: Start Thread T2 T1:启动线程T2
  2. T2: Do something T2:做点什么
  3. T1: Join with T2 T1:加入T2

With step 1 and 3 being Android live cycle events. 第1步和第3步是Android实时循环事件。 But because in the instrumentation test everything happens very fast I get: 但是因为在仪器测试中一切都发生得非常快我得到:

  1. T1: Start Thread T2 T1:启动线程T2
  2. T1: Join with T2 (which turn out to be a no-op) T1:加入T2(结果是无操作)
  3. T2: Do something T2:做点什么

Sure I could add a few sleeps to get the desired behaviour but I wonder if there is better way to do it. 当然,我可以添加一些睡眠来获得所需的行为,但我想知道是否有更好的方法来做到这一点。 iE is there a way to make sure the thread which was just start () -ed did actually start for good and is not still sitting in some scheduling queue awaiting start-up. iE有一种方法可以确保刚刚start () -ed的线程实际上确实启动并且仍未处于等待启动的某个调度队列中。

(Andy boy, do I miss Ada's rendezvous based multitasking) (安迪小子,我想念Ada基于会面的多任务处理)

And to answer mat's question: 并回答mat的问题:

  if (this.thread != null && this.thread.isAlive ())
  {
     this.stop.set (true);

     try
     {
        this.thread.join (1000);
     }
     catch (final InterruptedException Exception)
     {
        android.util.Log.w (Actor.TAG, "Thread did not want to join.", Exception);
     } // try
  } // if

As I said: no-op when because the thread has not started yet. 正如我所说:no-op因为线程尚未启动。

I typically use a CountDownLatch eg see this answer on testing asynchronous processes. 我通常使用CountDownLatch,例如,在测试异步进程时看到这个答案

If you want to synchronise the starting of many threads you can also use a CyclicBarrier . 如果要同步许多线程的启动,也可以使用CyclicBarrier

Martin, looking at your code I get the feeling that you may not be using the Thread class the way it was designed to be used. Martin,看着你的代码,我觉得你可能没有按照设计使用的方式使用Thread类。 In particular, testing whether the other thread is alive seems like an anti-pattern. 特别是,测试其他线程是否存活似乎是一种反模式。 In most practical scenarios you can omit the this.thread.isAlive () condition from your code, and the program will still work. 在大多数实际情况中,您可以从代码中省略this.thread.isAlive ()条件,程序仍然可以工作。

It seems that you're trying to make two threads (that should do two different things) run the same piece of code, and you use logical conditions (such as this.thread != null ) to decide which of the two threads is currently running. 看来你正在尝试使两个线程(应该做两个不同的事情)运行相同的代码,并使用逻辑条件(例如this.thread != null )来决定当前两个线程中的哪一个运行。

Typically, you'd write two classes each one extending Thread and implementing a run() method. 通常,您将编写两个类,每个类扩展Thread并实现run()方法。 Each run() method realizes the logic of a single thread. 每个run()方法都实现了单个线程的逻辑。 Then you'd launch the 2nd thread from the first, and call join() on that 2nd thread to wait for it to complete. 然后你从第一个线程启动第二个线程,并在第二个线程上调用join()以等待它完成。

public class SecondThread extends Thread {
  public void run() {
    ...
  }      
}

public class FirstThread extends Thread {
  public void run() {
    // Only FirstThread is running
    ...

    SecondThread st = new SecondThread();
    st.start();

    // Now both threads are running
    ...

    st.join(); // Wait for SecondThread to complete

    // Only FirstThread is running
    ...

  }                
}

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

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