1

The below code should fail according to the various users of this site. The documentation is either vague or does not directly address this. Why does this pass instead of waiting forever? Note code below is a JUnit test v4.12

@Test(timeout=10000)
public void test() {
    Thread t = new Thread(() -> {
        Thread curT = Thread.currentThread();
        try {
            curT.join();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    });
    t.start();
}
  • 5
    You start the thread and the method completes. JUnit will not wait for that thread to finish before it exits. So it only appears like `join` returned. – Sotirios Delimanolis Aug 15 '18 at 15:35
  • 1
    As you say, [other users say joining itself won't work](https://stackoverflow.com/a/5999598/2550406) and I interpret the docs the same way. However, you are actually starting a new thread, and join that one on itself. That means that your second thread keeps running forever, waiting for itself. But your main thread finishes. – lucidbrot Aug 15 '18 at 15:38

0 Answers0