// Example of multithreading in Java
class MyThread extends Thread {
private String threadName;
MyThread(String name) {
threadName = name;
}
@Override
public void run() {
for (int i = 1; i <= 5; i++) {
[Link](threadName + " - Count: "
+ i);
try {
[Link](500); // Pause for 500
milliseconds
} catch (InterruptedException e) {
[Link](threadName + "
interrupted.");
}
}
[Link](threadName + " finished.");
}
}
public class MultiThreadDemo {
public static void main(String[] args) {
MyThread thread1 = new MyThread("Thread 1");
MyThread thread2 = new MyThread("Thread 2");
[Link](); // Start Thread 1
[Link](); // Start Thread 2
}
}
Output