wang ming
wang ming

Reputation: 199

why the main thread is gone when invoke join method?

As per the Javadoc for Thread.State.TIMED_WAITING, I wrote code:

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();
    }
}}

and

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();
    }
}

the output is

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

Like that doc, the main thread steat was 'TIMED_WAITING', but it was gone, Where is the main thead?

PS: When I wrote

carThread.join();

It appeared

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

Upvotes: 0

Views: 99

Answers (1)

Antoniossss
Antoniossss

Reputation: 32535

Because without join your main thread finishes before other threads. join makes main thread "wait" until thread that join is invoked on finishes, blocking till that time.

Upvotes: 2

Related Questions