How can i print NEW state before the RUNNABLE, and TERMINATED state after the RUNNABLE? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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(); }

4th Jun 2022, 4:47 AM
Carl Salcedo
Carl Salcedo - avatar
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 } }
5th Jun 2022, 8:23 PM
zemiak