FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Operating-System-Lab/Threads/LAB2_2.java at main · ishan-gupt/Operating-System-Lab · GitHub
ishan-gupt
/
Operating-System-Lab
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
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
Operating-System-Lab
/
Threads
/
LAB2_2.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
116 lines (93 loc) · 2.86 KB
Breadcrumbs
Operating-System-Lab
/
Threads
/
LAB2_2.java
Copy path
File metadata and controls
116 lines (93 loc) · 2.86 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Java program for the above approach
import
java
.
util
.
Scanner
;
public
class
LAB2_2
{
static
int
factorial
(
int
n
) {
if
(
n
==
0
)
return
1
;
else
return
(
n
*
factorial
(
n
-
1
));
}
// Starting counter
int
counter
=
1
;
// Function to print odd numbers
public
void
printRows
(
int
N
) {
synchronized
(
this
) {
// Print number till the N
while
(
counter
<
N
) {
// If count is even then print
while
(
counter
%
2
==
0
) {
// Exception handle
try
{
wait
();
}
catch
(
InterruptedException
e
) {
e
.
printStackTrace
();
}
}
// Print the number
System
.
out
.
println
(
"T1 "
+
factorial
(
counter
) +
" "
);
// Increment counter
counter
++;
// Notify to second thread
notify
();
}
}
}
// Function to print even numbers
public
void
printColumns
(
int
N
) {
synchronized
(
this
) {
// Print number till the N
while
(
counter
<
N
) {
// If count is odd then print
while
(
counter
%
2
==
1
) {
// Exception handle
try
{
wait
();
}
catch
(
InterruptedException
e
) {
e
.
printStackTrace
();
}
}
// Print the number
System
.
out
.
println
(
"T2 "
+
factorial
(
counter
) +
" "
);
// Increment counter
counter
++;
// Notify to 2nd thread
notify
();
}
}
}
// Driver Code
public
static
void
main
(
String
[]
args
) {
Scanner
sc
=
new
Scanner
(
System
.
in
);
// Given Number N
System
.
out
.
println
(
"Enter the number or rows"
);
int
r
=
sc
.
nextInt
();
System
.
out
.
println
(
"Enter the number or columns"
);
int
c
=
sc
.
nextInt
();
int
arr
[][]=
new
int
[
r
][
c
];
for
(
int
i
=
0
;
i
<
r
;
i
++){
for
(
int
j
=
0
;
j
<
c
;
j
++){
System
.
out
.
println
(
"For "
+
i
+
" row and "
+
j
+
"column"
);
arr
[
i
][
j
]=
sc
.
nextInt
();
}
}
// Create an object of class
LAB2_1
mt
=
new
LAB2_1
();
// Create thread t1
Thread
t1
=
new
Thread
(
new
Runnable
() {
public
void
run
() {
mt
.
printRows
(
N
);
}
});
// Create thread t2
Thread
t2
=
new
Thread
(
new
Runnable
() {
public
void
run
() {
mt
.
printColumns
(
N
);
}
});
// Start both threads
t1
.
start
();
t2
.
start
();
}
}
Back
|
FazBrowse Home
|
New Git URL