Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
17 views

Lab - 5 Java

Uploaded by

Abhishek Kumar
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)
17 views

Lab - 5 Java

Uploaded by

Abhishek Kumar
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
You are on page 1/ 5

Shobhika

2200290120163
Java Lab -5 Assignment

15.
public class Main {
public static void main(String[] args) {
MyThread thread1 = new MyThread("Thread 1");
MyThread thread2 = new MyThread("Thread 2");

thread1.start();
thread2.start();
//TIP Press <shortcut actionId="ShowIntentionActions"/> with your caret
at the highlighted text
// to see how IntelliJ IDEA suggests fixing it.

}
static class MyThread extends Thread {
private String threadName;

MyThread(String name) {
this.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.");
}
}

Output -
16.
public class Main {
public static void main(String[] args) {
MyThread myRunnable1 = new MyThread("Thread 1");
MyThread myRunnable2 = new MyThread("Thread 2");

Thread thread1 = new Thread(myRunnable1);


Thread thread2 = new Thread(myRunnable2);

thread1.start();
thread2.start();

// Join threads to ensure main thread waits for their completion


try {
thread1.join();
thread2.join();
} catch (InterruptedException e) {
System.out.println("Main thread interrupted.");
}

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


}

static class MyThread implements Runnable {


private String threadName;

MyThread(String name) {
this.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.");
}
}
}

Output -

17.
public class Main {
public static void main(String[] args) {
MyThread myRunnable1 = new MyThread("Thread 1");
MyThread myRunnable2 = new MyThread("Thread 2");

Thread thread1 = new Thread(myRunnable1);


Thread thread2 = new Thread(myRunnable2);

// Set different priorities


thread1.setPriority(Thread.MIN_PRIORITY); // Priority 1
thread2.setPriority(Thread.MAX_PRIORITY); // Priority 10

thread1.start();
thread2.start();

// Join threads to ensure main thread waits for their completion


try {
thread1.join();
thread2.join();
} catch (InterruptedException e) {
System.out.println("Main thread interrupted.");
}

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


}

static class MyThread implements Runnable {


private String threadName;

MyThread(String name) {
this.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.");
}
}
}

Output -

You might also like