0% found this document useful (0 votes)
205 views3 pages

09 Hands On Activity 1 - ARG

This Java code defines a MyClass that extends the Thread class and overrides the run method. It then creates two MyClass thread objects, assigns them user-input names, and starts both threads running. It prints the initial states of the threads and then puts each thread to sleep for 500 milliseconds before checking and printing their states again.

Uploaded by

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

09 Hands On Activity 1 - ARG

This Java code defines a MyClass that extends the Thread class and overrides the run method. It then creates two MyClass thread objects, assigns them user-input names, and starts both threads running. It prints the initial states of the threads and then puts each thread to sleep for 500 milliseconds before checking and printing their states again.

Uploaded by

Chaé
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy