Implementation details of JAVA concurrent programming bounded cache
- 2020-05-19 04:54:05
- OfStack
JAVA concurrent programming implementation of bounded caching
1. Bounded cache base class
package cn.xf.cp.ch14;
/**
*
* Functionality: bounded cache implementation base class
* Time: afternoon 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 array
this.buf = (V[]) new Object[capacity];
}
// In the 1 A data ,final Method cannot be overridden
protected synchronized final void doPut(V v)
{
buf[tail] = v;
if(++tail == buf.length)
{
tail = 0;
}
// insert 1 Three methods, total amount ++
++count;
}
/**
* Take out the 1 A data
* @return
*/
protected synchronized final V doTake()
{
V v = buf[head];
buf[head] = null;
if(++head == buf.length)
{
head = 0;
}
--count;
return v;
}
// Based on the count To determine if the array is full
public synchronized final boolean isFull()
{
return count == buf.length;
}
public synchronized final boolean isEmpty()
{
return count == 0;
}
}
2. Determine the prerequisite and then execute the operation
package cn.xf.cp.ch14;
/**
*
* Function: first check the insert and get element operation, then perform the operation, verify not to pass no operation
* Time: afternoon 2:33:41
* 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, you cannot insert new elements
if(this.isFull())
{
throw new Exception(" The queue is beyond ");
}
this.doPut(v);
}
// Similarly, an empty queue cannot fetch a new element
public synchronized V take() throws Exception
{
if(this.isEmpty())
{
throw new Exception(" There are no elements in the queue ");
}
return this.doTake();
}
}
3, through polling and sleep to achieve simple blocking
package cn.xf.cp.ch14;
/**
*
* Functionality: simple blocking through polling and sleep
* Time: afternoon 2:55:54
* 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 you put it in the queue
public void put(V v) throws InterruptedException
{
while(true)
{
// There is no loop lock, otherwise the lock cannot be released, there is no sleep lock, there is no sleep lock, there is no sleep lock, there is no other person can operate when the sleep, there can never be an element out
synchronized (this)
{
// If the queue is not full, put in the element
if(!this.isFull())
{
this.doPut(v);
return;
}
}
// Otherwise hibernate and exit cpu Take up
Thread.sleep(SLEEP_GRANULARITY);
}
}
public V take() throws InterruptedException
{
while(true)
{
// There is no loop lock, otherwise the lock cannot be released, there is no hibernation lock, there is no hibernation lock, there is no hibernation lock, there is no other person can operate during hibernation, there can never be a new element to come in
synchronized(this)
{
// If the array part is empty, the data can be retrieved
if(!this.isEmpty())
{
return this.doTake();
}
// If the queue is empty, sleep for a few seconds and try again
}
Thread.sleep(SLEEP_GRANULARITY);
}
}
}
4. Conditional queue
package cn.xf.cp.ch14;
/**
*
* Function: use conditional queues
* Time: afternoon 3:32:04
* File: BoundedBuffer.java
*@author Administrator
*
* @param <V>
*/
public class BoundedBuffer<V> extends BaseBoundedBuffer<V>
{
public BoundedBuffer(int capacity)
{
super(capacity);
}
/**
* Put in the data element
* @param v
* @throws InterruptedException
*/
public synchronized void put(V v) throws InterruptedException
{
while(this.isFull())
{
// If you suspend the program here, it releases the lock
this.wait();
}
// If the queue is not full, the program wakes up and reacquires the lock
this.doPut(v);
// When execution ends, wake up the 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 to help you, thank you for your support of this site!