Skip to content
Snippets Groups Projects
Commit 3277d72e authored by Chao Zhan's avatar Chao Zhan
Browse files

add java example

parent b2c6dcac
Branches
No related tags found
No related merge requests found
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
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();
}
}
...@@ -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**:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment