-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwothread.java
38 lines (37 loc) · 1.03 KB
/
twothread.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
class AscendingThread extends Thread {
public void run() {
for (int i = 1; i <= 10; i++) {
System.out.println("Ascending Thread: " + i);
try {
Thread.sleep(1000); // pause for 1 second
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class DescendingThread extends Thread {
public void run() {
for (int i = 10; i >= 1; i--) {
System.out.println("Descending Thread: " + i);
try {
Thread.sleep(1000); // pause for 1 second
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class twothread {
public static void main(String[] args) {
AscendingThread ascendingThread = new AscendingThread();
DescendingThread descendingThread = new DescendingThread();
ascendingThread.start();
try {
ascendingThread.join(); // wait for the ascending thread to finish
} catch (InterruptedException e) {
e.printStackTrace();
}
descendingThread.start();
}
}