-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy path_8.Multithread.java
More file actions
42 lines (37 loc) · 985 Bytes
/
_8.Multithread.java
File metadata and controls
42 lines (37 loc) · 985 Bytes
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
import java.util.Random;
class X implements Runnable {
static int random;
Random rand = new Random();
public void run() {
random = rand.nextInt(25);
System.out.println(random);
}
}
class Y implements Runnable {
public void run() {
if (X.random % 2 == 0)
System.out.println((int) Math.pow(X.random, 2) + "\n");
}
}
class Z implements Runnable {
public void run() {
if (X.random % 2 != 0)
System.out.println((int) Math.pow(X.random, 3) + "\n");
}
}
class Multithread {
public static void main(String args[]) {
for (int i = 0; i < 10; i++) {
Thread objX = new Thread(new X());
Thread objY = new Thread(new Y());
Thread objZ = new Thread(new Z());
try {
Thread.sleep(1000);
} catch (Exception e) {
}
objX.start();
objY.start();
objZ.start();
}
}
}