KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gcc > util > InstancePool


1 /*
2  * Copyright 2004 The Apache Software Foundation or its licensors, as
3  * applicable.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14  * implied.
15  *
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19 package gcc.util;
20
21 import gcc.*;
22 import gcc.util.*;
23 import java.util.*;
24
25 public class InstancePool
26 {
27     // -----------------------------------------------------------------------
28
// inner classes
29
// -----------------------------------------------------------------------
30

31     private static class Entry
32     {
33         Object object;
34         long timeout;
35         Entry next;
36     }
37
38     // -----------------------------------------------------------------------
39
// private data
40
// -----------------------------------------------------------------------
41

42     private Entry _stack = null;
43
44     private Entry _freeList = null;
45
46     private long _idleTimeout;
47
48     // -----------------------------------------------------------------------
49
// public methods
50
// -----------------------------------------------------------------------
51

52     public InstancePool(String name)
53     {
54         init(name, 0); //, null);
55
}
56
57     public InstancePool(String name, long idleTimeout ) // , TimeoutObject timeoutObject)
58
{
59         init(name, idleTimeout); //, timeoutObject);
60
}
61
62     public Object get()
63     {
64         synchronized (this)
65         {
66             Entry top = _stack;
67             if (top != null)
68             {
69                 _stack = top.next;
70                 //_size.decrement();
71
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             //_size.increment();
89
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     // -----------------------------------------------------------------------
109
// private methods
110
// -----------------------------------------------------------------------
111

112     private void init(final String name, long idleTimeout) //, final TimeoutObject timeoutObject)
113
{
114         //_size = sizeStatistic.getInstance(name);
115
_idleTimeout = idleTimeout;
116
117         if (_idleTimeout > 0)
118         {
119             /*
120             long now = SystemClock.getLastSampleTime();
121             final long checkInterval = _idleTimeout > 10 ? (_idleTimeout / 10) : _idleTimeout;
122             Task timeoutTask = new Task()
123             {
124                 public long run(long time)
125                 {
126                     Entry restoreStack = null;
127                     Entry timeoutChain = null;
128                     synchronized (InstancePool.this)
129                     {
130                         while (_stack != null)
131                         {
132                             Entry entry = _stack;
133                             _stack = entry.next;
134                             if (entry.timeout > time)
135                             {
136                                 entry.next = restoreStack;
137                                 restoreStack = entry;
138                             }
139                             else
140                             {
141                                 entry.next = timeoutChain;
142                                 timeoutChain = entry;
143                                 _size.decrement();
144                             }
145                         }
146
147                         // Restore still-active entries to the stack in
148                         // their original order. This ensures that less
149                         // frequently used entries stay at the bottom of
150                         // the stack, becoming elegible for timeout.
151                         while (restoreStack != null)
152                         {
153                             Entry entry = restoreStack;
154                             restoreStack = entry.next;
155                             entry.next = _stack;
156                             _stack = entry;
157                         }
158                     }
159                     while (timeoutChain != null)
160                     {
161                         Entry entry = timeoutChain;
162                         timeoutChain = entry.next;
163                         try
164                         {
165                             timeoutObject.onTimeout(entry.object);
166                         }
167                         catch (Throwable ex)
168                         {
169                             ExceptionLog.getInstance().log(ex, InstancePool.class.getName() + ".onTimeout(" + name + ")");
170                         }
171                         entry.object = null;
172                         entry.next = null;
173                     }
174                     return time + checkInterval;
175                 }
176             }
177             ;
178             TaskScheduler.getInstance().start(timeoutTask, now);
179             */

180         }
181     }
182 }
183
Popular Tags