-
Notifications
You must be signed in to change notification settings - Fork 0
/
qb153.java
40 lines (35 loc) · 1.06 KB
/
qb153.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
/* QB 153:
Write a program to create two threads, one thread will print odd numbers and second
thread will print even numbers between 1 to 100 numbers
*/
class MyThread extends Thread {
public void run() {
if (Thread.currentThread().getName().equals("odd")) {
System.out.println("Odd: ");
for (int i = 1; i <= 100; i++) {
if (i % 2 != 0) {
System.out.print(i + ",");
}
}
System.out.println();
} else if (Thread.currentThread().getName().equals("even")) {
System.out.println("even");
for (int i = 1; i <= 100; i++) {
if (i % 2 == 0) {
System.out.print(i + ",");
}
}
}
}
}
class QB153 {
public static void main(String[] args) throws InterruptedException {
MyThread t1 = new MyThread();
t1.setName("odd");
t1.start();
t1.join();
MyThread t2 = new MyThread();
t2.setName("even");
t2.start();
}
}