JAVA Concurrent Programming Bounded Cache Implementation
1. Base class of bounded cache
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | package cn.xf.cp.ch14;
public class BaseBoundedBuffer<V>
{
private final V[] buf;
private int tail;
private int head;
private int count ;
public BaseBoundedBuffer(int capacity)
{
this.buf = (V[]) new Object[capacity];
}
protected synchronized final void doPut(V v)
{
buf[tail] = v;
if (++tail == buf.length)
{
tail = 0;
}
++ count ;
}
protected synchronized final V doTake()
{
V v = buf[head];
buf[head] = null;
if (++head == buf.length)
{
head = 0;
}
-- count ;
return v;
}
public synchronized final boolean isFull()
{
return count == buf.length;
}
public synchronized final boolean isEmpty()
{
return count == 0;
}
}
|
Copy after login
2. Determination of prerequisites Perform operations
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | package cn.xf.cp.ch14;
public class GrumpyBoundedBuffer<V> extends BaseBoundedBuffer<V>
{
public GrumpyBoundedBuffer(int size)
{
super(size);
}
public synchronized void put(V v) throws Exception
{
if (this.isFull())
{
throw new Exception( "队列超出" );
}
this.doPut(v);
}
public synchronized V take() throws Exception
{
if (this.isEmpty())
{
throw new Exception( "队列中无元素" );
}
return this.doTake();
}
}
|
Copy after login
3. Implement simple blocking through polling and sleep
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | package cn.xf.cp.ch14;
public class SleepyBoundedBuffer<V> extends BaseBoundedBuffer<V>
{
private static final long SLEEP_GRANULARITY = 2000;
public SleepyBoundedBuffer(int capacity)
{
super(capacity);
}
public void put(V v) throws InterruptedException
{
while (true)
{
synchronized (this)
{
if (!this.isFull())
{
this.doPut(v);
return ;
}
}
Thread.sleep(SLEEP_GRANULARITY);
}
}
public V take() throws InterruptedException
{
while (true)
{
synchronized(this)
{
if (!this.isEmpty())
{
return this.doTake();
}
}
Thread.sleep(SLEEP_GRANULARITY);
}
}
}
|
Copy after login
4. Conditional queue
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | package cn.xf.cp.ch14;
public class BoundedBuffer<V> extends BaseBoundedBuffer<V>
{
public BoundedBuffer(int capacity)
{
super(capacity);
}
public synchronized void put(V v) throws InterruptedException
{
while (this.isFull())
{
this.wait();
}
this.doPut(v);
this.notifyAll();
}
public synchronized V take() throws InterruptedException
{
while (this.isEmpty())
{
this.wait();
}
V v = this.doTake();
this.notifyAll();
return v;
}
}
|
Copy after login
Thank you for reading, I hope it can help everyone, thank you for your support of this site!
For more detailed explanations on the implementation of bounded cache in JAVA concurrent programming, please pay attention to the PHP Chinese website!