From e9d2aff1fdc4f197c1cc38e9c07ffede640aac9a Mon Sep 17 00:00:00 2001 From: Karsten Jeppesen Date: Wed, 26 Aug 2020 14:25:42 +0200 Subject: [PATCH] 2. Concurrency Semaphore Demo --- SemaphoreDemo/.classpath | 6 ++ SemaphoreDemo/.project | 17 ++++ .../.settings/org.eclipse.jdt.core.prefs | 11 +++ SemaphoreDemo/src/ProCon.java | 88 +++++++++++++++++++ SemaphoreDemo/src/SemaphoreDemo.java | 35 ++++++++ 5 files changed, 157 insertions(+) create mode 100644 SemaphoreDemo/.classpath create mode 100644 SemaphoreDemo/.project create mode 100644 SemaphoreDemo/.settings/org.eclipse.jdt.core.prefs create mode 100644 SemaphoreDemo/src/ProCon.java create mode 100644 SemaphoreDemo/src/SemaphoreDemo.java diff --git a/SemaphoreDemo/.classpath b/SemaphoreDemo/.classpath new file mode 100644 index 0000000..51a8bba --- /dev/null +++ b/SemaphoreDemo/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/SemaphoreDemo/.project b/SemaphoreDemo/.project new file mode 100644 index 0000000..1c2fe94 --- /dev/null +++ b/SemaphoreDemo/.project @@ -0,0 +1,17 @@ + + + ProCon + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/SemaphoreDemo/.settings/org.eclipse.jdt.core.prefs b/SemaphoreDemo/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..3a21537 --- /dev/null +++ b/SemaphoreDemo/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/SemaphoreDemo/src/ProCon.java b/SemaphoreDemo/src/ProCon.java new file mode 100644 index 0000000..f168a81 --- /dev/null +++ b/SemaphoreDemo/src/ProCon.java @@ -0,0 +1,88 @@ +// java program to demonstrate +// use of semaphores Locks +import java.util.concurrent.*; + +//A shared resource/class. +class Shared +{ + static int count = 0; +} + +class MyThread extends Thread +{ + // We need 2 semaphores. + // This one will prevent the Producer writing twice + Semaphore semPro; + // This one will prevent the consumer reading twice + Semaphore semCon; + String threadName; + + public MyThread(Semaphore mySemPro, Semaphore mySemCon, String threadName) + { + super(threadName); + // We set the semaphores from the main method + this.semPro = mySemPro; + this.semCon = mySemCon; + this.threadName = threadName; + } + + @Override + public void run() + { + + // run by thread "Producer" + if(this.getName().equals("Producer")) + { + System.out.println("Starting " + threadName); + for ( int i=0; i<5; i++) + { + try + { + // Wait until we may write (initially we are allowed) + // coming back here - we must wait for the "Consumer" to read + semPro.acquire(); + Shared.count++; + System.out.println(threadName + " Writes: " + Shared.count); + // Signal the "Consumer" that a value is ready + semCon.release(); + // Now, allowing a context switch -- if possible. + // for thread Consumer to execute + Thread.sleep(10); + + } + catch (InterruptedException exc) + { + // Should anything fail - then we end up here + System.out.println(exc); + } + } + // And we are done + System.out.println("Ending " + threadName); + } + + // run by thread Consumer + else + { + System.out.println("Starting " + threadName); + for (int i=0; i< 5; i++) + { + try + { + // Waiting for a value to be present (initially: Wait) + semCon.acquire(); + System.out.println(threadName + " Reads: " + Shared.count); + // Signal the "Producer" that the value was read, so the buffer is now cleared + semPro.release(); + } + catch (InterruptedException exc) + { + // Should we fail - here we go + System.out.println(exc); + } + } + // And we are done + System.out.println("Ending " + threadName); + } + } +} + diff --git a/SemaphoreDemo/src/SemaphoreDemo.java b/SemaphoreDemo/src/SemaphoreDemo.java new file mode 100644 index 0000000..7b05c13 --- /dev/null +++ b/SemaphoreDemo/src/SemaphoreDemo.java @@ -0,0 +1,35 @@ +// java program to demonstrate +// use of semaphores Locks +import java.util.concurrent.*; + + +// Driver class +public class SemaphoreDemo +{ + public static void main(String args[]) throws InterruptedException + { + // creating a Semaphore object + // A 1 means that you are allowed 1 write to start with + Semaphore semPro = new Semaphore(1); + // A 0 means that you are not allowed to read (as nobody has yet written to the buffer) + Semaphore semCon = new Semaphore(0); + + // creating two threads with name A and B + // Note that thread A will increment the count + // and thread B will decrement the count + MyThread mt1 = new MyThread(semPro, semCon, "Producer"); + MyThread mt2 = new MyThread(semPro, semCon, "Consumer"); + + // stating threads A and B + mt1.start(); + mt2.start(); + + // waiting for threads A and B + mt1.join(); + mt2.join(); + + // count will always remain 0 after + // both threads will complete their execution + System.out.println("count: " + Shared.count); + } +}