From 3277d72e67dcb583aaa8f6d31e354c2ee2de9df8 Mon Sep 17 00:00:00 2001
From: Chao Zhan <chao.zhan@fu-berlin.de>
Date: Wed, 14 Jun 2023 18:21:21 +0200
Subject: [PATCH] add java example

---
 exercises/monitor_java/Makefile               |  22 ++++
 .../monitor_java/SynchronizedCounter.java     |  46 +++++++
 slides/pages/recap.md                         | 115 ++++++++++++++++++
 3 files changed, 183 insertions(+)
 create mode 100644 exercises/monitor_java/Makefile
 create mode 100644 exercises/monitor_java/SynchronizedCounter.java

diff --git a/exercises/monitor_java/Makefile b/exercises/monitor_java/Makefile
new file mode 100644
index 0000000..530fdc8
--- /dev/null
+++ b/exercises/monitor_java/Makefile
@@ -0,0 +1,22 @@
+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
diff --git a/exercises/monitor_java/SynchronizedCounter.java b/exercises/monitor_java/SynchronizedCounter.java
new file mode 100644
index 0000000..3417640
--- /dev/null
+++ b/exercises/monitor_java/SynchronizedCounter.java
@@ -0,0 +1,46 @@
+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();
+    }
+}
diff --git a/slides/pages/recap.md b/slides/pages/recap.md
index 2405cb3..12fa0b6 100644
--- a/slides/pages/recap.md
+++ b/slides/pages/recap.md
@@ -16,6 +16,121 @@ title: Recap I
 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
 
 **The runtime of a program**:
-- 
GitLab