Implementation of JAVA concurrent programming bounded cache
1. Base class of bounded cache
package cn.xf.cp.ch14;/** * Function: Bounded cache implements base class * Time: 2:20:00 pm * 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 in a data, the final method cannot be rewritten protected synchronized final void doPut(V v) { buf[tail] = v; if(++tail == buf.length) { tail = 0; } //Insert a method, total amount++ ++count; } /** * Take out a data* @return */ protected synchronized final V doTake() { V v = buf[head]; buf[head] = null; if(++head == buf.length) { head = 0; } --count; return v; } //By judging the count, determine whether the array is full public synchronized final boolean isFull() { 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: Check the insertion and obtain element operations first, and then perform the operation. The verification does not pass and will not be allowed to operate * 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 it is a full queue, the new element cannot be inserted if(this.isFull()) { throw new Exception("Qule exceeds"); } this.doPut(v); } //Similarly, if the queue is empty, the new element cannot be retrieved public synchronized V take() throws Exception { if(this.isEmpty()) { throw new Exception("No element in the queue"); } return this.doTake(); }}3. Achieve simple blocking through polling and sleeping
package cn.xf.cp.ch14;/** * Function: implement simple blocking through polling and hibernation*Time: 2:55:54 pm *File: SleepyBoundedBuffer.java *@author Administrator * * @param <V> */public class SleepyBoundedBuffer<V> extends BaseBoundedBuffer<V>{ //2s private static final long SLEEP_GRANULARITY = 2000; public SleepyBoundedBuffer(int capacity) { super(capacity); } //When putting in the queue public void put(V v) throws InterruptedException { while(true) { //The loop is not locked here, otherwise the lock will not be released. If the hibernation is not locked, sleep is locked, and others cannot operate it during hibernation. It is impossible for an element to go out synchronized (this) { //If the queue is not full, then put the element if(!this.isFull()) { this.doPut(v); return; } } //Otherwise, sleep and exit the CPU to occupy Thread.sleep(SLEEP_GRANULARITY); } } public V take() throws InterruptedException { while(true) { //The loop is not locked here, otherwise the lock will not be released. If the sleep is not locked, the sleep is locked, and others cannot operate it when sleeping. There will never be new elements coming in synchronized(this) { //If the array part is empty, then the data can be retrieved if(!this.isEmpty()) { return this.doTake(); } //If the queue is empty, try sleeping for a few seconds again} Thread.sleep(SLEEP_GRANULARITY); } } }4. Conditional queue
package cn.xf.cp.ch14;/** * Function: Use 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 data elements* @param v * @throws InterruptedException */ public synchronized void put(V v) throws InterruptedException { while(this.isFull()) { //Suspend the program here and the lock will be released this.wait(); } //If the queue is not full, then the program is awakened and the lock will be regained this.doPut(v); //The execution ends and wakes up other queues this.notifyAll(); } public synchronized V take() throws InterruptedException { while(this.isEmpty()) { this.wait(); } V v = this.doTake(); this.notifyAll(); return v; } }Thank you for reading, I hope it can help you. Thank you for your support for this site!