[JAVA concurrent programming practice] 11. Implementation of bounded cache and java practice
1. bounded cache base class
Package cn. xf. cp. ch14;/*** function: bounded cache implementation base class * Time: 2:20:00 * file: BaseBoundedBuffer. java * @ author Administrator ** @ param <V> */public class BaseBoundedBuffer <V> {private final V [] buf; private int tail; private int head; private int count; public BaseBoundedBuffer (int capacity) {// initialize the array this. buf = (V []) new Object [capacity];} // put a data, the final method cannot be overwritten protected synchronized final void doPut (V v) {buf [tail] = v; if (++ tail = buf. length) {tail = 0 ;}// insert a method, total number of + + count ;} /*** retrieve a data ** @ return */protected synchronized final V doTake () {V v = buf [head]; buf [head] = null; if (++ head = buf. length) {head = 0 ;}-- count; return v ;}// determine whether the array is full public synchronized final boolean isFull () through count () {return count = buf. length;} public synchronized final boolean isEmpty () {return count = 0 ;}}
2. Determine the prerequisites before performing the operation
Package cn. xf. cp. ch14;/*** function: checks the insert and retrieve element operations first, and then performs the operation. If the verification fails, the operation is rejected. * Time: 2:33:41 pm * file: GrumpyBoundedBuffer. java * @ author Administrator ** @ param <V> */public class GrumpyBoundedBuffer <V> extends BaseBoundedBuffer <V> {public GrumpyBoundedBuffer (int size) {super (size );} public synchronized void put (V v) throws Exception {// if the queue is full, a new element if (this. isFull () {throw new Exception ("queue exceeded");} this. doPut (v);} // Similarly, if the queue is empty, the new element public synchronized V take () throws Exception {if (this. isEmpty () {throw new Exception ("no element in queue");} return this. doTake ();}}
3. Simple blocking through polling and sleep
Package cn. xf. cp. ch14;/*** function: simple blocking through polling and sleep * Time: 2:55:54 * file: SleepyBoundedBuffer. java * @ author Administrator ** @ param <V> */public class SleepyBoundedBuffer <V> extends BaseBoundedBuffer <V >{// 2 s private static final long SLEEP_GRANULARITY = 2000; public SleepyBoundedBuffer (int capacity) {super (capacity);} // public void put (V v) throws InterruptedException {while (true) {// The loop is not locked here Otherwise, the lock will not be released. No sleep lock, sleep lock, no one else can operate during sleep, and there will never be any element going out synchronized (this) {// if the queue is not full, put the element if (! This. isFull () {this. doPut (v); return; }}// otherwise, sleep and the cpu usage stops. sleep (SLEEP_GRANULARITY) ;}} public V take () throws InterruptedException {while (true) {// The loop lock is not performed here, otherwise the lock cannot be released and the sleep lock is not performed, sleep locks, and no one else can operate during sleep. There will never be any new elements coming in synchronized (this) {// if the array is empty, you can retrieve the data if (! This. isEmpty () {return this. doTake () ;}// if the queue is empty, sleep for several seconds and try again} Thread. sleep (SLEEP_GRANULARITY );}}}
4. Condition queue
Package cn. xf. cp. ch14;/*** function: Use the condition queue * Time: 3:32:04 pm * file: BoundedBuffer. java * @ author Administrator ** @ param <V> */public class BoundedBuffer <V> extends BaseBoundedBuffer <V> {public BoundedBuffer (int capacity) {super (capacity );} /*** put the data element * @ param v * @ throws InterruptedException */public synchronized void put (V v) throws InterruptedException {while (this. isFull () {// here the program is suspended and the lock this will be released. wait ();} // If the queue is not full, the lock will be acquired again after the program is awakened. doPut (v); // The execution ends, and this is used to wake up other queues. policyall ();} public synchronized V take () throws InterruptedException {while (this. isEmpty () {this. wait ();} V v = this. doTake (); this. policyall (); return v ;}}