-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathMultipleThreads.java
More file actions
71 lines (63 loc) · 1013 Bytes
/
MultipleThreads.java
File metadata and controls
71 lines (63 loc) · 1013 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import java.util.*;
class UseRandom extends Thread
{
public void run()
{
int num = 0;
Random r = new Random();
try{
for(int i=0;i<5;i++)
{
num = r.nextInt(50);
System.out.println("Generated no. is "+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 Even implements Runnable
{
public int x;
public Even(int x)
{
this.x = x;
}
public void run()
{
System.out.println("New Thread"+x+" is even and Square = "+(x*x));
}
}
class Odd implements Runnable
{
public int x;
public Odd(int x)
{
this.x = x;
}
public void run()
{
System.out.println("New Thread"+x+" is Odd and Cube is ="+(x*x*x));
}
}
public class MultipleThreads
{
public static void main(String s[])
{
UseRandom a = new UseRandom();
a.start();
}
}