-
Notifications
You must be signed in to change notification settings - Fork 0
/
multithreading.java
44 lines (37 loc) · 1.12 KB
/
multithreading.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
class MyThread extends Thread {
public MyThread(String name) {
super(name);
}
@Override
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println(Thread.currentThread().getName() + ": Count - " + i);
// Simulate some work
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class multithreading {
public static void main(String[] args) {
MyThread thread1 = new MyThread("Thread 1");
MyThread thread2 = new MyThread("Thread 2");
// Setting priorities
thread1.setPriority(Thread.MIN_PRIORITY); // Minimum priority
thread2.setPriority(Thread.MAX_PRIORITY); // Maximum priority
// Starting threads
thread1.start();
thread2.start();
// Wait for threads to finish
try {
thread1.join();
thread2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Main Thread exiting.");
}
}