-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreadsqcube.java
More file actions
69 lines (62 loc) · 938 Bytes
/
threadsqcube.java
File metadata and controls
69 lines (62 loc) · 938 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
package Lab;
import java.util.*;
class cube extends Thread
{
int x;
cube(int n)
{
x=n;
}
public void run()
{
System.out.println("Cube "+x+" = "+(Math.pow(x, 3)));
}
}
class square extends Thread
{
int x;
square(int n)
{
x=n;
}
public void run()
{
System.out.println("Square of "+x+" = "+Math.pow(x, 2));
}
}
class Number extends Thread
{
synchronized void solve()
{
Random r=new Random();
for(int i=0;i<5;i++) {
int n=r.nextInt(100);
System.out.println("Random integer: "+n);
if(n%2==0)
{
square s=new square(n);
s.start();
}
else
{
cube c=new cube(n);
c.start();
}}
try
{
Thread.sleep(100);}
catch(InterruptedException e)
{
System.out.println(e);
}
}
public void run() {
solve();}
}
public class threadsqcube {
public static void main(String[] args)
// TODO Auto-generated method stub
{
Number n=new Number();
n.start();
}}