-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathOddEven.java
More file actions
57 lines (49 loc) · 937 Bytes
/
OddEven.java
File metadata and controls
57 lines (49 loc) · 937 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import java.util.Random;
class Even implements Runnable{
public int x;
public Even(int x){
this.x=x;
}
public void run(){
System.out.println("Even thread,square of " + x + " = " + (x*x));
}
}
class Odd implements Runnable{
public int x;
public Odd(int x){
this.x=x;
}
public void run(){
System.out.println("Odd thread,cube of " + x + " = " + (x*x*x));
}
}
class USE extends Thread{
public void run(){
int num = 0;
Random r = new Random();
try{
for(int i=0;i<5;i++){
num = r.nextInt(20);
System.out.println("Generated no: " + num);
if(num%2==0){
Thread t1 = new Thread(new Even(num));
t1.start();
}
else{
Thread t2 = new Thread(new Odd(num));
t2.start();
}
Thread.sleep(1000);
}
}
catch(Exception e){
System.out.println(e.getMessage());
}
}
}
class OddEven{
public static void main(String args[]){
USE a = new USE();
a.start();
}
}