1 18 package org.apache.geronimo.interop.util; 19 20 21 22 23 public class InstancePool { 24 28 private static class Entry { 29 Object object; 30 long timeout; 31 Entry next; 32 } 33 34 38 private Entry _stack = null; 39 40 private Entry _freeList = null; 41 42 private long _idleTimeout; 43 44 48 public InstancePool(String name) { 49 init(name, 0); } 51 52 public InstancePool(String name, long idleTimeout) { 54 init(name, idleTimeout); } 56 57 public Object get() { 58 synchronized (this) { 59 Entry top = _stack; 60 if (top != null) { 61 _stack = top.next; 62 Object object = top.object; 64 top.object = null; 65 top.next = _freeList; 66 _freeList = top; 67 return object; 68 } else { 69 return null; 70 } 71 } 72 } 73 74 public void put(Object object) { 75 synchronized (this) { 76 Entry top = _freeList; 78 if (top != null) { 79 _freeList = top.next; 80 } else { 81 top = new Entry(); 82 } 83 top.object = object; 84 if (_idleTimeout > 0) { 85 top.timeout = System.currentTimeMillis() + _idleTimeout; 86 } 87 top.next = _stack; 88 _stack = top; 89 } 90 } 91 92 96 private void init(final String name, long idleTimeout) { 98 _idleTimeout = idleTimeout; 100 101 if (_idleTimeout > 0) { 102 163 } 164 } 165 } 166 | Popular Tags |