1 19 package gcc.util; 20 21 import gcc.*; 22 import gcc.util.*; 23 import java.util.*; 24 25 public class InstancePool 26 { 27 31 private static class Entry 32 { 33 Object object; 34 long timeout; 35 Entry next; 36 } 37 38 42 private Entry _stack = null; 43 44 private Entry _freeList = null; 45 46 private long _idleTimeout; 47 48 52 public InstancePool(String name) 53 { 54 init(name, 0); } 56 57 public InstancePool(String name, long idleTimeout ) { 59 init(name, idleTimeout); } 61 62 public Object get() 63 { 64 synchronized (this) 65 { 66 Entry top = _stack; 67 if (top != null) 68 { 69 _stack = top.next; 70 Object object = top.object; 72 top.object = null; 73 top.next = _freeList; 74 _freeList = top; 75 return object; 76 } 77 else 78 { 79 return null; 80 } 81 } 82 } 83 84 public void put(Object object) 85 { 86 synchronized (this) 87 { 88 Entry top = _freeList; 90 if (top != null) 91 { 92 _freeList = top.next; 93 } 94 else 95 { 96 top = new Entry(); 97 } 98 top.object = object; 99 if (_idleTimeout > 0) 100 { 101 top.timeout = System.currentTimeMillis() + _idleTimeout; 102 } 103 top.next = _stack; 104 _stack = top; 105 } 106 } 107 108 112 private void init(final String name, long idleTimeout) { 114 _idleTimeout = idleTimeout; 116 117 if (_idleTimeout > 0) 118 { 119 180 } 181 } 182 } 183 | Popular Tags |