+ 1
How can i print NEW state before the RUNNABLE, and TERMINATED state after the RUNNABLE?
This is my code public class Multi extends Thread { public void run() { try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Thread: " + this.getName() + " state: " + this.getState()); } public static void main(String args[]) { Scanner inputScanner = new Scanner(System.in); System.out.println("Enter Thread 1 Name:"); String name1 = inputScanner.nextLine(); System.out.println("Enter Thread 2 Name:"); String name2 = inputScanner.nextLine(); Multi t1 = new Multi(); t1.setName(name1); t1.start(); Multi t2 = new Multi(); t2.setName(name2); t2.start(); }
1 Answer
0
import java.util.*;
public class Multi extends Thread {
public void run() { }
void printState() {
System.out.println(
"Thread: " +this.getName() +
" state: " +this.getState() );
}
public static void main(String args[])
throws InterruptedException {
Multi t1 = new Multi();
t1.setName("A");
t1.printState(); //NEW
t1.start();
t1.printState(); //RUNNABLE
t1.join();
t1.printState(); //TERMINATED
}
}