-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMultiTh.java
More file actions
57 lines (56 loc) · 1.24 KB
/
MultiTh.java
File metadata and controls
57 lines (56 loc) · 1.24 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
51
52
53
54
55
56
57
//Write a Java program that implements a multi-threaded program which has three threads.
//First thread generates a random integer every 1 second.
//If the value is even, second thread computes the square of the number and prints.
// If the value is odd the third thread will print the value of cube of the number.
import java.util.Random;
class RandomThread extends Thread
{
public void run()
{
Random r = new Random();
for(int i=0;i<20;i++)
{
int n=r.nextInt(100);//i will get the value between 0 & 100
if(n%2==0)
{
new Even(n).start();
}
else
{
new Odd(n).start();
}
}
}
}
class Even extends Thread
{
private int num;
public Even(int num)
{
this.num=num;
}
public void run()
{
System.out.println("Square of "+num+"="+ num*num);
}
}
class Odd extends Thread
{
private int num;
public Odd(int num)
{
this.num=num;
}
public void run()
{
System.out.println("Cube of "+num+"="+num*num*num);
}
}
class MultiTh
{
public static void main(String args[])
{
RandomThread r=new RandomThread();
r.start();
}
}