-
Notifications
You must be signed in to change notification settings - Fork 3
/
Main2.java
41 lines (36 loc) · 1.01 KB
/
Main2.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
//LEC ONE
//THE THIRD EXAMPLE: CREATING MULT-THREADS USING DIFF CLASSES BY IMPLEMENTING RUNNABLE
public class Main2 {
public static void main(String[] args) {
ThreadA ta = new ThreadA();
ThreadB tb = new ThreadB();
ThreadC tc = new ThreadC();
Thread a = new Thread(ta);
Thread b = new Thread(tb);
Thread c = new Thread(tc);
a.start();
b.start();
c.start();
}
}
class ThreadA implements Runnable {
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println("ThreadA: " + i); // replace i with (i*2) to get even numbers
}
}
}
class ThreadB implements Runnable {
public void run() {
for (int j = 1; j <= 5; j++) {
System.out.println("ThreadB: " + j); // replace j with (j*2-1) to get odd numbers
}
}
}
class ThreadC implements Runnable {
public void run() {
for (int k = 1; k <= 5; k++) {
System.out.println("ThreadC: " + k);
}
}
}