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