为什么调用join方法时主线程消失了?

时间:2016-09-22 10:07:17

标签: java multithreading

根据Thread.State.TIMED_WAITING的Javadoc,我编写了代码:

import java.time.LocalDateTime;
import java.util.concurrent.TimeUnit;

public class TestJoin {

public static void main(String[] args) throws InterruptedException {
    ThreadUtil.printThreadState("main", "car", "dog");

    Thread carThread = new Thread(() -> {
        System.out.println(LocalDateTime.now() + " Car run");
        long now = System.currentTimeMillis();
        while (true) {
            if (System.currentTimeMillis() - now > 3000) {
                break;
            }
        }
        System.out.println(LocalDateTime.now() + " Car Stop");
    }, "car");

    Thread dogThread = new Thread(() -> {
        System.out.println(LocalDateTime.now() + " Dog run");
        long now = System.currentTimeMillis();
        while (true) {
            if (System.currentTimeMillis() - now > 3000) {
                break;
            }
        }
        System.out.println(LocalDateTime.now() + " Dog Stop");
    }, "dog");

    try {
        dogThread.start();
        carThread.start();

        System.out.println("Begin join");
        carThread.join(3);
        System.out.println("endjoin");
    } catch (Exception e) {
        e.printStackTrace();
    }
}}

import java.time.LocalDateTime;

public class ThreadUtil {

    public static void printThreadState(String... filter) {
        Thread print = new Thread(() -> {
            long now = System.currentTimeMillis();
            while (true) {
                if (System.currentTimeMillis() - now > 1000) {
                    now = System.currentTimeMillis();
                    Thread.getAllStackTraces().forEach((key, thread) -> {
                        for (int i = 0; i < filter.length; i++) {
                            if (key.getName().equals(filter[i])) {
                                System.out.println(LocalDateTime.now() + "  " +key.getName() + " -> " + key.getState());
                            }
                        }
                    });
                }
            }
        }, "Print");
        print.start();
    }
}

输出

Begin join
end join
2016-09-22T18:01:53.484 Car run
2016-09-22T18:01:53.498 Dog run
2016-09-22T18:01:54.460  dog -> RUNNABLE
2016-09-22T18:01:54.460  car -> RUNNABLE
2016-09-22T18:01:55.531  dog -> RUNNABLE
2016-09-22T18:01:55.532  car -> RUNNABLE
2016-09-22T18:01:56.461  dog -> RUNNABLE
2016-09-22T18:01:56.462  car -> RUNNABLE
2016-09-22T18:01:56.486 Car Stop
2016-09-22T18:01:56.499 Dog Stop

就像那个文档一样,主要的线程是&#39; TIMED_WAITING&#39;,但它已经消失了,主要的那里是什么?

PS: 我写的时候

carThread.join();

出现了

Begin join
2016-09-22T18:04:31.583 Dog run
2016-09-22T18:04:31.584 Car run
2016-09-22T18:04:32.543  main -> WAITING
2016-09-22T18:04:32.543  dog -> RUNNABLE
2016-09-22T18:04:32.543  car -> RUNNABLE
2016-09-22T18:04:33.604  main -> WAITING
2016-09-22T18:04:33.604  dog -> RUNNABLE
2016-09-22T18:04:33.604  car -> RUNNABLE
2016-09-22T18:04:34.585 Car Stop
end join
2016-09-22T18:04:34.608 Dog Stop
2016-09-22T18:04:34.608  dog -> TERMINATED

1 个答案:

答案 0 :(得分:2)

因为没有join主线程在其他线程之前完成。 join使主线程#34;等待&#34;直到在完成时调用join的线程,阻塞到那个时间。