Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
ALP4-Tutorials
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
mactavish96
ALP4-Tutorials
Commits
3277d72e
Commit
3277d72e
authored
2 years ago
by
Chao Zhan
Browse files
Options
Downloads
Patches
Plain Diff
add java example
parent
b2c6dcac
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
exercises/monitor_java/Makefile
+22
-0
22 additions, 0 deletions
exercises/monitor_java/Makefile
exercises/monitor_java/SynchronizedCounter.java
+46
-0
46 additions, 0 deletions
exercises/monitor_java/SynchronizedCounter.java
slides/pages/recap.md
+115
-0
115 additions, 0 deletions
slides/pages/recap.md
with
183 additions
and
0 deletions
exercises/monitor_java/Makefile
0 → 100644
+
22
−
0
View file @
3277d72e
JFLAGS
=
-g
JC
=
javac
JVM
=
java
.SUFFIXES
:
.java .class
.java.class
:
$(
JC
)
$(
JFLAGS
)
$*
.java
MAIN
=
SynchronizedCounter
CLASSES
=
SynchronizedCounter.java
default
:
classes
classes
:
$(CLASSES:.java=.class)
.PHONY
:
run clean
run
:
$(MAIN).class
$(
JVM
)
$(
MAIN
)
clean
:
$(
RM
)
*
.class
This diff is collapsed.
Click to expand it.
exercises/monitor_java/SynchronizedCounter.java
0 → 100644
+
46
−
0
View file @
3277d72e
class
Count
{
// synchronized block
synchronized
void
displayCounting
(
int
n
,
int
threadId
)
{
for
(
int
i
=
1
;
i
<=
n
;
i
++)
{
System
.
out
.
printf
(
"Thread %d: %d\n"
,
threadId
,
i
);
try
{
// sleep for 500 milliseconds
Thread
.
sleep
(
500
);
}
catch
(
Exception
e
)
{
System
.
out
.
println
(
e
);
}
}
}
}
// Thread 1
class
Thread_A
extends
Thread
{
Count
c
;
Thread_A
(
Count
c
)
{
this
.
c
=
c
;
}
public
void
run
()
{
c
.
displayCounting
(
5
,
1
);
}
}
// Thread 2
class
Thread_B
extends
Thread
{
Count
c
;
Thread_B
(
Count
c
)
{
this
.
c
=
c
;
}
public
void
run
()
{
c
.
displayCounting
(
5
,
2
);
}
}
public
class
SynchronizedCounter
{
public
static
void
main
(
String
args
[])
{
Count
obj
=
new
Count
();
Thread_A
t1
=
new
Thread_A
(
obj
);
Thread_B
t2
=
new
Thread_B
(
obj
);
t1
.
start
();
t2
.
start
();
}
}
This diff is collapsed.
Click to expand it.
slides/pages/recap.md
+
115
−
0
View file @
3277d72e
...
@@ -16,6 +16,121 @@ title: Recap I
...
@@ -16,6 +16,121 @@ title: Recap I
title: Recap II
title: Recap II
---
---
## Monitor
Monitor is construct that
**contains**
shared data structures, operations, and synchronization between concurrent procedure calls.
Monitors provide a
**high level**
of synchronization between processes.
<v-click>
### Components of Monitor
</v-click>
<br/>
<v-clicks>
-
Initialization/Destruction
-
Private Data, Locks, Condition Variables
-
Monitor Procedures/Interfaces
-
Monitor Entry Queue (managed by
**condition variables**
)
</v-clicks>
<v-click>
**Advantages and Disadvantages of Monitors?**
</v-click>
<v-click>
More details in the
[
original paper
](
https://dl.acm.org/doi/pdf/10.1145/355620.361161
)
from C.A.R. Hoare.
</v-click>
---
title
:
Recap III - Monitor Java Example I
---
### Monitor Example in Java
The following Java example code explains the synchronization via monitor:
```
java
class
Count
{
// synchronized block
synchronized
void
displayCounting
(
int
n
)
{
for
(
int
i
=
1
;
i
<=
n
;
i
++)
{
System
.
out
.
println
(
i
);
try
{
// sleep for 500 milliseconds
Thread
.
sleep
(
500
);
}
catch
(
Exception
e
)
{
System
.
out
.
println
(
e
);
}
}
}
}
```
---
title: Recap III - Monitor Java Example II
---
### Monitor Example in Java (cont.)
```
java
// Thread 1
class
Thread_A
extends
Thread
{
Count
c
;
Thread_A
(
Count
c
)
{
this
.
c
=
c
;
}
public
void
run
()
{
c
.
displayCounting
(
5
);
}
}
// Thread 2
class
Thread_B
extends
Thread
{
Count
c
;
Thread_B
(
Count
c
)
{
this
.
c
=
c
;
}
public
void
run
()
{
c
.
displayCounting
(
5
);
}
}
```
---
title: Recap III - Monitor Java Example III
---
### Monitor Example in Java (cont.)
```
java
public
class
main
{
public
static
void
main
(
String
args
[])
{
Count
obj
=
new
Count
();
Thread_A
t1
=
new
Thread_A
(
obj
);
Thread_B
t2
=
new
Thread_B
(
obj
);
t1
.
start
();
t2
.
start
();
}
}
```
See live demo.
---
title: Recap IV
---
### Amdahl's Law
### Amdahl's Law
**The runtime of a program**
:
**The runtime of a program**
:
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment