Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*************************************************
- * Semaphore Class implemented using synchronized *
- * & the system calls wait() & notify() *
- * ============================================== *
- * Name: Nantipat Tullwattana *
- * Major: B.Sc. of Software Engineering *
- *************************************************/
- public class Semaphore
- {
- public Semaphore(int i){ value=i;}
- /*Wait() implemented as synchronized to ensure that it is done atomically*/
- public synchronized void Wait()
- {
- /*decremnt the semaphore*/
- value--;
- /*force the process to wait if value<0 */
- if(value<0)
- wait();
- }
- /*Signal() is implemented as synchronized to ensure it is done atomically*/
- public synchronized void Signal()
- {
- value++; /* increment semaphore value*/
- /*wake up any waiting processes*/
- if(value<=0)
- notify();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement