0% found this document useful (0 votes)
6 views1 page

MyThread Extends Thread

thhh

Uploaded by

rayannhuertas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views1 page

MyThread Extends Thread

thhh

Uploaded by

rayannhuertas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

import java.util.

Scanner;

class MyThread extends Thread {


public MyThread(String name) {
super(name);
}

@Override
public void run() {
System.out.println(getName() + " is " + getState());
}
}

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

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


String thread1Name = scanner.nextLine();
System.out.print("Name your second thread: ");
String thread2Name = scanner.nextLine();

MyThread thread1 = new MyThread(thread1Name);


MyThread thread2 = new MyThread(thread2Name);

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


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

System.out.println("Starting the threads...");


thread1.start();
thread2.start();

try {
Thread.sleep(500); // sleep for half a second
} catch (InterruptedException e) {
e.printStackTrace();
}

System.out.println("After sleep...");
System.out.println(thread1.getName() + " is " + thread1.getState());
System.out.println(thread2.getName() + " is " + thread2.getState());
}
}

You might also like