0% found this document useful (0 votes)
3 views2 pages

Multi Threading

The document provides an example of multithreading in Java using a custom thread class 'MyThread'. It demonstrates how to create and start multiple threads that print a count from 1 to 5 with a pause of 500 milliseconds between each count. The main class 'MultiThreadDemo' initiates two threads, 'Thread 1' and 'Thread 2', which execute concurrently.

Uploaded by

Ishwari Gole
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)
3 views2 pages

Multi Threading

The document provides an example of multithreading in Java using a custom thread class 'MyThread'. It demonstrates how to create and start multiple threads that print a count from 1 to 5 with a pause of 500 milliseconds between each count. The main class 'MultiThreadDemo' initiates two threads, 'Thread 1' and 'Thread 2', which execute concurrently.

Uploaded by

Ishwari Gole
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

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

You might also like