KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jofti > store > HelperManager


1
2 package com.jofti.store;
3
4 import com.jofti.core.IStoreManager;
5 import com.jofti.exception.JoftiException;
6
7 import edu.emory.mathcs.backport.java.util.concurrent.LinkedBlockingQueue;
8
9 /**
10  * Pool of Externalizable helpers to obtain and release helpers to and from.
11  * <p>
12  * @author xenephon
13  *
14  */

15 public class HelperManager {
16
17     LinkedBlockingQueue freeQueue = null;
18     IStoreManager manager =null;
19     int nodeSize;
20     int blockSize;
21     /**
22      *
23      */

24     public HelperManager() {
25     }
26     
27     public void init(int helperNumber,int blockSize, int nodeSize,IStoreManager manager ) throws JoftiException{
28 // set up the buffer pool
29
freeQueue = new LinkedBlockingQueue(helperNumber);
30         this.manager =null;
31         this.blockSize = blockSize;
32         this.nodeSize =nodeSize;
33         // set up the helpers
34
for (int i=0;i<helperNumber;i++){
35             ExternalisableHelper helper = new ExternalisableHelper();
36             helper.init(nodeSize, blockSize,manager);
37             freeQueue.add(helper);
38         }
39     }
40
41     
42     public ExternalisableHelper acquireHelper()
43     throws JoftiException
44     {
45
46         ExternalisableHelper helper = null;
47
48         helper =(ExternalisableHelper) freeQueue.poll();
49         if (helper == null){
50             helper = new ExternalisableHelper();
51             helper.init(nodeSize, blockSize,manager);
52         }
53         return helper;
54     }
55
56     public void releaseHelper(ExternalisableHelper helper)
57     {
58             freeQueue.offer(helper);
59     }
60 }
61
Popular Tags