Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

OOPS LAB 2

Name: ARYAN SAWANT

Reg No: 21BCE8189

Q1) WAP in java to implement multi-threading.

ANS)

class MyThread extends Thread {

private String threadName;

MyThread(String name) {

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.");

public class MultiThreadingDemo1 {

public static void main(String[] args) {

MyThread t1 = new MyThread("Thread 1");

MyThread t2 = new MyThread("Thread 2");


t1.start();

t2.start();

try {

t1.join(); // Wait for t1 to finish

t2.join(); // Wait for t2 to finish

} catch (InterruptedException e) {

System.out.println("Main thread interrupted.");

System.out.println("Main thread exiting.");

You might also like