package com.example.domain;

class SecondThread extends Thread {

	public SecondThread(String name) {
		super(name);
		System.out.println("New thread: " + name);
	}

	@Override
	public void run() {
		try {
			for (int i = 0; i < 5; i++) {
				System.out.println("Thread " + Thread.currentThread().getName() + " is running...");
				Thread.sleep(3000);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
}

public class TestThread2 {

	public static void main(String[] args) {
		// create thread objects
		SecondThread t1 = new SecondThread("T1");
		SecondThread t2 = new SecondThread("T2");
		SecondThread t3 = new SecondThread("T3");

		// start thread one by one
		t1.start();
		// called join() on thread
		try {
			System.out.println("Calling join() on threads");
			t1.join();
		} catch (Exception e) {
			// TODO: handle exception
		}

		System.out.println("T1 is alive: " + t1.isAlive());
		System.out.println("T2 is alive: " + t2.isAlive());
		System.out.println("T3 is alive: " + t3.isAlive());

		t2.start();
		// called join() on thread
		try {
			System.out.println("Calling join() on threads");
			t2.join();
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		// check the threads again
		System.out.println("T1 is alive: " + t1.isAlive());
		System.out.println("T2 is alive: " + t2.isAlive());
		System.out.println("T3 is alive: " + t3.isAlive());
		
		t3.start();
		// called join() on thread
		try {
			System.out.println("Calling join() on threads");
			t3.join();
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		// check the threads again
		System.out.println("T1 is alive: " + t1.isAlive());
		System.out.println("T2 is alive: " + t2.isAlive());
		System.out.println("T3 is alive: " + t3.isAlive());
	}

}
