Back to project page coursera-android-pos.
The source code is released under:
MIT License
If you think the Android project coursera-android-pos listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package edu.vuum.mocca; //from ww w. j a v a 2s .co m import java.util.concurrent.locks.ReentrantLock; import java.util.concurrent.locks.Condition; /** * @class SimpleSemaphore * * @brief This class provides a simple counting semaphore * implementation using Java a ReentrantLock and a * ConditionObject (which is accessed via a Condition). It must * implement both "Fair" and "NonFair" semaphore semantics, * just liked Java Semaphores. */ public class SimpleSemaphore { /** * Define a ReentrantLock to protect the critical section. */ // TODO - you fill in here private ReentrantLock mLock = new ReentrantLock(); /** * Define a Condition that waits while the number of permits is 0. */ // TODO - you fill in here private Condition isZero = mLock.newCondition(); /** * Define a count of the number of available permits. */ // TODO - you fill in here. Make sure that this data member will // ensure its values aren't cached by multiple Threads.. private int mPermits; public SimpleSemaphore(int permits, boolean fair) { // TODO - you fill in here to initialize the SimpleSemaphore, // making sure to allow both fair and non-fair Semaphore // semantics. mPermits = permits; mLock = new ReentrantLock(fair); isZero = mLock.newCondition(); } /** * Acquire one permit from the semaphore in a manner that can be * interrupted. */ public void acquire() throws InterruptedException { // TODO - you fill in here. mLock.lock(); while (mPermits == 0) isZero.await(); mPermits--; mLock.unlock(); } /** * Acquire one permit from the semaphore in a manner that cannot be * interrupted. */ public void acquireUninterruptibly() { // TODO - you fill in here. mLock.lock(); while (mPermits == 0){ isZero.awaitUninterruptibly(); } mPermits--; mLock.unlock(); } /** * Return one permit to the semaphore. */ void release() { // TODO - you fill in here. mLock.lock(); try { mPermits++; isZero.signal(); } finally { mLock.unlock(); } } /** * Return the number of permits available. */ public int availablePermits() { // TODO - you fill in here by changing null to the appropriate // return value. return mPermits; } }