01 /*
02 * Copyright (c) 1995-2010, The University of Sheffield. See the file
03 * COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
04 *
05 * This file is part of GATE (see http://gate.ac.uk/), and is free
06 * software, licenced under the GNU Library General Public License,
07 * Version 2, June 1991 (in the distribution as file licence.html,
08 * and also available at http://gate.ac.uk/gate/licence.html).
09 *
10 * Atanas Kiryakov, 01/02/2002
11 *
12 * $Id: ObjectPool.java 12006 2009-12-01 17:24:28Z thomas_heitz $
13 */
14 package gate.util;
15
16
17 import java.util.Vector;
18
19 /**
20 A generic implementation of pool of references to objects of any kind.
21 It is thread-safe, so, allows multiple users to get and release objects
22 "simultaneously". In fact, the standard Java synchronization is used.
23 <BR><BR>
24 The base idea is that, a calling routine will try to get
25 an object from the pool with method Get. On success, it will use the
26 object and return it in the pool with the method Put.
27 <BR><BR>
28 If there ares no available objects in the pool, Get will return <B>null</B>.
29 Then the calling routine should create a new object. Further, scenario goes
30 in the same way - when finished using the object, calling routine shoud Put
31 it in the pool for future use, instead of leaving it to be garbage-collected.
32 <BR><BR>
33 The pool initially is empty. The only way to increase the number of objects
34 managed by the pool, is some external process to Put an object, that was
35 created, instead of previously Get from the pool.
36 <BR><BR>
37 Pool stores only references to currently "free" or available objects. When
38 some external routine Gets an object from the pool, its reference is not
39 locked, it is simply removed from the pool.
40 */
41 public class ObjectPool {
42 private Vector objects;
43 private int size;
44
45 /**
46 Constructs and object pool with specified size.
47 @param size determines the maximum size of the pool. This is the number
48 free objects that it can manage at the same time
49 */
50 public ObjectPool(int size) {
51 this.size = size;
52 objects = new Vector(this.size);
53 } // ObjectPool
54
55 /**
56 Pulls out an object from the pool. The reference to the object is removed
57 from the pool and their is no longer any kind of relation between this
58 object and the pool. It can be returned back (released) by Put method.
59 @return an object from the pool, if available.<BR>
60 Otherwise, returns <B>null</B>
61 */
62 public synchronized Object get(){
63 int n = objects.size();
64 if (n > 0){
65 Object o = objects.elementAt(n-1);
66 objects.removeElementAt(n-1);
67 return o;
68 }
69 else
70 return null;
71 } // Get
72
73 /**
74 Puts an object in the pool, those stating that it's "free" or available
75 for use by another processes and routines. An object can be put in the pool,
76 without need to be get from there before.
77 @return <B>true</B> on success<BR>
78 <B>false</B> when the object was not put in the pool,
79 because the maximal number of references in the pool was riched
80 */
81 public synchronized boolean put(Object o){
82 if (objects.size() > 30)
83 return false;
84 objects.addElement(o);
85 return true;
86 } // Put
87 }
|