FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
java-programs/Multithread_Application/multithread.java at main · IntScription/java-programs · GitHub
IntScription
/
java-programs
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
java-programs
/
Multithread_Application
/
multithread.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
90 lines (73 loc) · 2.11 KB
Breadcrumbs
java-programs
/
Multithread_Application
/
multithread.java
Copy path
File metadata and controls
90 lines (73 loc) · 2.11 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
85
86
87
88
89
/*
Write a Java program that implements a multithreaded 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 the cube of the number.
*/
package
Multithread_Application
;
import
java
.
util
.
Random
;
import
java
.
util
.
concurrent
.
ExecutorService
;
import
java
.
util
.
concurrent
.
Executors
;
import
java
.
util
.
concurrent
.
TimeUnit
;
class
Square
implements
Runnable
{
private
int
num
;
Square
(
int
n
) {
num
=
n
;
}
public
void
printSquare
() {
System
.
out
.
println
(
"Square of "
+
num
+
" is "
+ (
num
*
num
));
}
public
void
run
() {
printSquare
();
}
}
class
Cube
implements
Runnable
{
private
int
num
;
Cube
(
int
n
) {
num
=
n
;
}
public
void
printCube
() {
System
.
out
.
println
(
"Cube of "
+
num
+
" is "
+ (
num
*
num
*
num
));
}
public
void
run
() {
printCube
();
}
}
class
RandInt
extends
Thread
{
private
final
ExecutorService
executor
=
Executors
.
newFixedThreadPool
(
2
);
public
void
printRandom
()
throws
InterruptedException
{
Random
rd
=
new
Random
();
while
(
true
) {
int
rnum
=
rd
.
nextInt
(
10
);
System
.
out
.
println
(
"Random Number is "
+
rnum
);
Thread
.
sleep
(
3000
);
executor
.
submit
(
new
Square
(
rnum
));
executor
.
submit
(
new
Cube
(
rnum
));
}
}
public
void
run
() {
try
{
printRandom
();
}
catch
(
InterruptedException
e
) {
}
}
public
void
shutdown
() {
executor
.
shutdown
();
}
public
void
awaitTermination
()
throws
InterruptedException
{
executor
.
awaitTermination
(
Long
.
MAX_VALUE
,
TimeUnit
.
NANOSECONDS
);
}
}
public
class
multithread
{
public
static
void
main
(
String
[]
args
) {
RandInt
ob1
=
new
RandInt
();
ob1
.
start
();
try
{
Thread
.
sleep
(
15000
);
// Run for 15 seconds
ob1
.
shutdown
();
ob1
.
awaitTermination
();
}
catch
(
InterruptedException
e
) {
e
.
printStackTrace
();
}
}
}
Back
|
FazBrowse Home
|
New Git URL