-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
50 lines (45 loc) · 1.37 KB
/
Main.java
File metadata and controls
50 lines (45 loc) · 1.37 KB
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
45
46
47
48
49
50
import java.util.Random;
class ThreadHelloWorld extends Thread {
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println("Hello World " + i);
try {
Thread.sleep(new Random().nextInt(400) + 100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class ThreadMultiplication extends Thread {
public void run() {
for (int i = 1; i <= 5; i++) {
int result = 2 * i;
System.out.println("2 * " + i + " = " + result);
try {
Thread.sleep(new Random().nextInt(400) + 100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class Main {
public static void main(String[] args) {
ThreadHelloWorld threadHelloWorld = new ThreadHelloWorld();
ThreadMultiplication threadMultiplication = new ThreadMultiplication();
if (new Random().nextBoolean()) {
threadHelloWorld.start();
threadMultiplication.start();
} else {
threadMultiplication.start();
threadHelloWorld.start();
}
try {
threadHelloWorld.join();
threadMultiplication.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}