-
Notifications
You must be signed in to change notification settings - Fork 0
/
Multithreading
61 lines (59 loc) · 1.08 KB
/
Multithreading
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
import java.util.*;
class Square extends Thread{
int x;
Square(int n)
{
x=n;
}
public void run()
{
int sqr=x*x;
System.out.println("square is"+sqr);
}
}
class Cube extends Thread{
int x;
Cube(int n)
{
x=n;
}
public void run()
{
int cub=x*x*x;
System.out.println("cube is"+cub);
}
}
class Number extends Thread
{
Random rd=new Random();
public void run()
{
for(int i=0;i<10;i++)
{
int a= rd.nextInt(100);
System.out.println("generater num is"+a);
Square s=new Square(a);
Cube c=new Cube(a);
if(a%2==0)
{
s.start();
}
else
c.start();
try{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
System.out.println(e);
}
}
}
}
public class Mthread {
public static void main(String args[])
{
Number n=new Number();
n.start();
}
}