SemaphoreDemo: Refactored.
This commit is contained in:
1
Technology/SemaphoreDemo/.gitignore
vendored
Normal file
1
Technology/SemaphoreDemo/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
/bin/
|
||||||
@@ -1,5 +1,15 @@
|
|||||||
// java program to demonstrate
|
// Karsten Jeppesen, UCN
|
||||||
// use of semaphores Locks
|
//
|
||||||
|
// java application to demonstrate
|
||||||
|
// use of semaphores (Locks)
|
||||||
|
//
|
||||||
|
// Semaphore methods:
|
||||||
|
// acquire()
|
||||||
|
// will wait for semaphore to be in released state. Will set semaphore to acquired state
|
||||||
|
// release()
|
||||||
|
// will release an acquired semaphore
|
||||||
|
|
||||||
|
|
||||||
import java.util.concurrent.*;
|
import java.util.concurrent.*;
|
||||||
|
|
||||||
//A shared resource/class.
|
//A shared resource/class.
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
// java program to demonstrate
|
// Karsten Jeppesen, UCN
|
||||||
// use of semaphores Locks
|
//
|
||||||
|
// java application to demonstrate
|
||||||
|
// use of semaphores (Locks)
|
||||||
|
|
||||||
import java.util.concurrent.*;
|
import java.util.concurrent.*;
|
||||||
|
|
||||||
|
|
||||||
@@ -14,9 +17,7 @@ public class SemaphoreDemo
|
|||||||
// A 0 means that you are not allowed to read (as nobody has yet written to the buffer)
|
// A 0 means that you are not allowed to read (as nobody has yet written to the buffer)
|
||||||
Semaphore semCon = new Semaphore(0);
|
Semaphore semCon = new Semaphore(0);
|
||||||
|
|
||||||
// creating two threads with name A and B
|
// creating two threads named "Producer" and "Consumer"
|
||||||
// Note that thread A will increment the count
|
|
||||||
// and thread B will decrement the count
|
|
||||||
MyThread mt1 = new MyThread(semPro, semCon, "Producer");
|
MyThread mt1 = new MyThread(semPro, semCon, "Producer");
|
||||||
MyThread mt2 = new MyThread(semPro, semCon, "Consumer");
|
MyThread mt2 = new MyThread(semPro, semCon, "Consumer");
|
||||||
|
|
||||||
|
|||||||
1
Technology/ThreadsDemo/.gitignore
vendored
Normal file
1
Technology/ThreadsDemo/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
/bin/
|
||||||
Reference in New Issue
Block a user