Lab - 5 Java
Lab - 5 Java
2200290120163
Java Lab -5 Assignment
15.
public class Main {
public static void main(String[] args) {
MyThread thread1 = new MyThread("Thread 1");
MyThread thread2 = new MyThread("Thread 2");
thread1.start();
thread2.start();
//TIP Press <shortcut actionId="ShowIntentionActions"/> with your caret
at the highlighted text
// to see how IntelliJ IDEA suggests fixing it.
}
static class MyThread extends Thread {
private String threadName;
MyThread(String name) {
this.threadName = name;
}
@Override
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println(threadName + " - Count: " + i);
try {
Thread.sleep(500); // Sleep for 500 milliseconds
} catch (InterruptedException e) {
System.out.println(threadName + " interrupted.");
}
}
System.out.println(threadName + " exiting.");
}
}
Output -
16.
public class Main {
public static void main(String[] args) {
MyThread myRunnable1 = new MyThread("Thread 1");
MyThread myRunnable2 = new MyThread("Thread 2");
thread1.start();
thread2.start();
MyThread(String name) {
this.threadName = name;
}
@Override
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println(threadName + " - Count: " + i);
try {
Thread.sleep(500); // Sleep for 500 milliseconds
} catch (InterruptedException e) {
System.out.println(threadName + " interrupted.");
}
}
System.out.println(threadName + " exiting.");
}
}
}
Output -
17.
public class Main {
public static void main(String[] args) {
MyThread myRunnable1 = new MyThread("Thread 1");
MyThread myRunnable2 = new MyThread("Thread 2");
thread1.start();
thread2.start();
MyThread(String name) {
this.threadName = name;
}
@Override
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println(threadName + " - Count: " + i);
try {
Thread.sleep(500); // Sleep for 500 milliseconds
} catch (InterruptedException e) {
System.out.println(threadName + " interrupted.");
}
}
System.out.println(threadName + " exiting.");
}
}
}
Output -