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

Java Multi-Threading Example Code

The document presents a Java program that demonstrates multi-threading by creating two threads using the Thread class. Each thread prints numbers from 1 to 5 with a 0.5-second delay between prints. The main class starts both threads concurrently, showcasing their simultaneous execution.

Uploaded by

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

Java Multi-Threading Example Code

The document presents a Java program that demonstrates multi-threading by creating two threads using the Thread class. Each thread prints numbers from 1 to 5 with a 0.5-second delay between prints. The main class starts both threads concurrently, showcasing their simultaneous execution.

Uploaded by

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

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
}
}

You might also like