Finals OOP Hands On Act

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

import java.util.

Scanner;
public class ThreadDemo implements Runnable{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
ThreadDemo td = new ThreadDemo();

Thread T1 = new Thread(td);


Thread T2 = new Thread(td);
Thread T3 = new Thread(td);
Thread T4 = new Thread(td);

System.out.print("Name Your First Thread: ");


String name1 = sc.nextLine();
System.out.print("Name Your Second Thread: ");
String name2 = sc.nextLine();
System.out.print("Name Your Third Thread: ");
String name3 = sc.nextLine();
System.out.print("Name Your Fourth Thread: ");
String name4 = sc.nextLine();

T1.setName(name1);
T2.setName(name2);
T3.setName(name3);
T4.setName(name4);

System.out.println("\n" + T1.getName() + " is " + T1.getState() + "\n" +


T2.getName() + " is " + T2.getState() +"\n" + T3.getName() + " is " + T3.getState()
+"\n" + T4.getName() + " is " + T4.getState() );

System.out.println("\n ===Starting threads===");

T1.start();
T2.start();
T3.start();
T4.start();

try{
T1.sleep(500);
T2.sleep(1000);
T3.sleep(1500);
T4.sleep(2000);

}catch(Exception e){
e.printStackTrace();

}
System.out.println("\n===After Sleep===");
System.out.println(T1.getName() + " is " + T1.getState() + "\n" +
T2.getName() + " is " + T2.getState() +"\n" + T3.getName() + " is " + T3.getState()
+"\n" + T4.getName() + " is " + T4.getState() );
}
@Override
public void run(){

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


Thread.currentThread(). getState());

}
}

You might also like