2. Concurrency
Semaphore Demo
This commit is contained in:
6
SemaphoreDemo/.classpath
Normal file
6
SemaphoreDemo/.classpath
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
||||
17
SemaphoreDemo/.project
Normal file
17
SemaphoreDemo/.project
Normal file
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>ProCon</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
11
SemaphoreDemo/.settings/org.eclipse.jdt.core.prefs
Normal file
11
SemaphoreDemo/.settings/org.eclipse.jdt.core.prefs
Normal file
@@ -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
|
||||
88
SemaphoreDemo/src/ProCon.java
Normal file
88
SemaphoreDemo/src/ProCon.java
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
35
SemaphoreDemo/src/SemaphoreDemo.java
Normal file
35
SemaphoreDemo/src/SemaphoreDemo.java
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user