package com.example.domain;

// MyThread is a subclass of Thread (superclass)
public class MyThread extends Thread {

	static int counter = 0;

	// constructor
	// method automatically called when create new object
	// constructor must have the same name as class name
	public MyThread() {
		super("Demo Thread" + counter); // calling superclass constructor
		System.out.println("MyThread() executed...");
		System.out.println("Child: " + counter);
		// this refers to current object (MyThread object)
		start(); // start the thread
		// increment counter
		counter++;
	}

	// since Thread implements Runnable
	// Thread has run() method to be implemented/override
	@Override
	public void run() {
		try {
			for (int i = 0; i < 5; i++) {
				System.out.println("Child thread: " + 
						Thread.currentThread().getName() + " - " + i);
				Thread.sleep(1000);
			}
		} catch (Exception e) {
			// do nothing
		}
		System.out.println("Exiting child thread..." + Thread.currentThread().getName());
	}
}
