-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmulti thread application.txt
84 lines (68 loc) · 1.49 KB
/
multi thread application.txt
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
72
73
74
75
76
77
78
79
80
81
82
83
84
Write a Java program that implements a multi-thread application that has three threads.
First thread generates a random integer for every 1 second;
second thread computes the square of the number and prints; third thread will print the value of cube of the number.
import java.util.Random;
class SquareThread implements Runnable
{
int x;
SquareThread(int x)
{
this.x = x;
}
public void run()
{
System.out.println("Thread Name:Square Thread and Square of " + x + " is:
" + x * x);
}
}
class CubeThread implements Runnable
{
int x;
CubeThread(int x)
{
this.x = x;
}
public void run()
{
System.out.println("Thread Name:Cube Thread and Cube of " + x + " is: " +
x * x * x);
}
}
class RandomThread implements Runnable
{
Random r;
Thread t2, t3;
public void run()
{
int num;
r = new Random();
try
{
while (true)
{
num = r.nextInt(100);
System.out.println("Main Thread and Generated Number is "
+ num);
t2 = new Thread(new SquareThread(num));
t2.start();
t3 = new Thread(new CubeThread(num));
t3.start();
Thread.sleep(1000);
System.out.println("--------------------------------------");
}
}
catch (Exception ex)
{
System.out.println("Interrupted Exception");
}
}
}
public class MainThread
{
public static void main(String[] args)
{
RandomThread thread_obj = new RandomThread();
Thread t1 = new Thread(thread_obj);
t1.start();
}
}