Java Program to Demonstrate Multi-Threading Operation This program creates two threads using the
Thread class and runs them concurrently. Each thread prints numbers from 1 to 5 with a delay.
Java Code:
// Java Program to Demonstrate Multi-Threading Operation
class MyThread extends Thread {
public void run() {
for (int i = 1; i <= 5; i++) {
[Link]([Link]().getName() + " : " + i);
try {
[Link](500); // pause for 0.5 sec
} catch (Exception e) {
[Link](e);
}
}
}
}
public class MultiThreadDemo {
public static void main(String[] args) {
MyThread t1 = new MyThread();
MyThread t2 = new MyThread();
[Link]("Thread-1");
[Link]("Thread-2");
[Link](); // starting first thread
[Link](); // starting second thread
}
}