Implementation of bounded cache for Java concurrent programming
1, bounded cache of the base class
Package cn.xf.cp.ch14; /** * * Function: Bounded cache implementation base class * Time: 2:20:00 * File: Baseboundedbuffer.java * @author Administrator * * @param <V> * * publi
C class Baseboundedbuffer<v> {private final v[] buf;
private int tail;
private int head;
private int count;
public baseboundedbuffer (int capacity) {//initialization array this.buf = (v[]) new object[capacity];
The final method cannot be overridden 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;
//through the judgment of count, to determine whether the array is full public synchronized final Boolean isfull () {return count = = Buf.length;
Public Synchronized Final Boolean IsEmpty () {return count = 0;
}
}
2, to determine the prerequisite to carry out the operation
Package cn.xf.cp.ch14;
/**
*
* function: First check the insert and get element operations, then perform the operation, verify not through not operation
* Time: PM 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 the new element
if (This.isfull ())
{
throw new Exception ("queue exceeded");
}
This.doput (v);
}
Similarly, you cannot remove new elements public
synchronized V take () throws Exception
{
if (This.isempty ())
{
throw new Exception ("No elements in Queue");
return This.dotake ();
}
3, through polling and hibernation to achieve simple blocking
Package cn.xf.cp.ch14;
/** * * Function: Simple blocking through polling and hibernation * Time: 2:55:54 * File: Sleepyboundedbuffer.java * @author Administrator * @param <V> * * public class Sleepyboundedbuffer<v> extends baseboundedbuffer<v> {//2s private static final long Slee
p_granularity = 2000;
public sleepyboundedbuffer (int capacity) {super (capacity); ///When placed in queue public void putting (V V) throws Interruptedexception {while (true) {//This is not locked in the loop, otherwise this lock can't be released. , not to sleep locked, dormant locked, in hibernation when others can not operate, can never have elements out synchronized (this) {//If the queue is not full, then put the element if (!this.isful
L ()) {this.doput (v);
Return
}//Otherwise hibernate, Exit CPU occupancy Thread.Sleep (sleep_granularity); The public V take () throws Interruptedexception {while (true) {//is not locked in the loop, otherwise this lock cannot be released, no sleep locked, dormant locked,
No one else can operate in hibernation, and there will never be a new element in it. Synchronized (this) {//If the array is empty, you can remove the data if (!this.isempty ())
{ return This.dotake ();
///If the queue is empty, hibernate for a few seconds and try} thread.sleep (sleep_granularity);
}
}
}
4. Conditional queue
Package cn.xf.cp.ch14;
/**
*
* Function: Use conditional queue
* Time: PM 3:32:04
* File: Boundedbuffer.java
* @author Administrator
* * param <V>
* * Public
class Boundedbuffer<v> extends baseboundedbuffer<v>
{
public boundedbuffer (int capacity)
{
super (capacity);
}
/**
* into the data element
* @param v
* @throws interruptedexception
/public
synchronized void put (v V) Throws Interruptedexception
{
while (This.isfull ())
{
///Here hangs the program, releasing the Lock
this.wait ();
}
If the queue is not full, then the program is awakened to acquire a new lock
this.doput (v);
End of execution, wake 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 to help you, thank you for your support for this site!