You are on page 1of 3

import java.util.

Scanner;

/**

*/

public class MyClass extends Thread {

public static void main(String[] args) {

MyClass thread1 = new MyClass();

MyClass thread2 = new MyClass();

String t_name1, t_name2;

Scanner xiao = new Scanner(System.in);

System.out.print("Name your first thread: ");

t_name1 = xiao.nextLine();

System.out.print("Name your second thread: ");

t_name2 = xiao.nextLine();

thread1.setName(t_name1);

thread2.setName(t_name2);

System.out.println(thread1.getName() + " is " + thread1.getState());

System.out.println(thread2.getName() + " is " + thread2.getState());

System.out.println("\n\nStarting the threads...");

thread1.start();

thread2.start();
try {

thread1.sleep(500);

thread2.sleep(500);

System.out.println("\n\nAfter sleep...");

System.out.println(thread1.getName() + " is " + Thread.State.TERMINATED);

System.out.println(thread2.getName() + " is " + Thread.State.TERMINATED);

} catch(InterruptedException e) {

System.out.println("\n\nAfter the thread is running...");

System.out.println(thread1.getName() + " is " + thread1.getState());

System.out.println(thread2.getName() + " is " + thread2.getState());

public void run() {

System.out.println(Thread.currentThread().getName() + " is " + Thread.currentThread().getState());

OUTPUT:

You might also like