001: /**
002: * $Id: ParallelObjectPool.java,v 1.9 2005/12/02 13:53:25 as133206 Exp $
003: * Copyright 2002 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.common.pool;
014:
015: import com.sun.portal.desktop.monitoring.PoolStatistic;
016:
017: public class ParallelObjectPool implements Pool {
018: private int _minSize;
019: private int _maxSize;
020: private int _partitions;
021: private Pool[] _pools;
022: private boolean _destroyed;
023: private int[] _partitionUse;
024: static private int _pnum = 0;
025:
026: public ParallelObjectPool(ObjectManager objectManager, int minSize,
027: int maxSize, boolean overflow, int partitions) {
028: if (partitions < 1) {
029: throw new IllegalArgumentException(
030: "ParallelObjectPool.<init>: partitions<1");
031: }
032: _minSize = minSize;
033: _maxSize = maxSize;
034: _partitions = partitions;
035: _pools = new Pool[partitions];
036:
037: int partitionMinSize = minSize / partitions;
038: int partitionMaxSize = maxSize / partitions;
039: int partitionMinRest = minSize % partitions;
040: int partitionMaxRest = maxSize % partitions;
041: int min = partitionMinSize
042: + (((partitionMinRest--) > 0) ? 1 : 0);
043: int max = partitionMaxSize
044: + (((partitionMaxRest--) > 0) ? 1 : 0);
045: for (int i = 0; i < partitions; i++) {
046: _pools[i] = new SimpleObjectPool(objectManager, min, max,
047: overflow);
048: min = partitionMinSize
049: + (((partitionMinRest--) > 0) ? 1 : 0);
050: max = partitionMaxSize
051: + (((partitionMaxRest--) > 0) ? 1 : 0);
052: }
053: _partitionUse = new int[partitions];
054: }
055:
056: private synchronized int calculatePartition() {
057: return (_pnum++) % _partitions;
058: }
059:
060: public Object obtainObject(Object param) {
061: if (_destroyed) {
062: throw new IllegalStateException();
063: }
064: int partition = calculatePartition();
065: Pool pool = _pools[partition];
066: Object o = pool.obtainObject(param);
067: if (o == null) {
068: for (int i = 0; o == null && i < _partitions; i++) {
069: if (i != partition) {
070: pool = _pools[i];
071: o = pool.obtainObject(param);
072: if (o != null) {
073: partition = i;
074: }
075: }
076: }
077: }
078: if (o != null && (o instanceof PartitionObject)) {
079: ((PartitionObject) o).setPartition(partition);
080: _partitionUse[partition]++;
081: }
082: if (poolStatistic != null) {
083: poolStatistic.setCurrent(getLeased());
084: }
085: return o;
086: }
087:
088: public void releaseObject(Object o) {
089: int partition;
090: if (o instanceof PartitionObject) {
091: partition = ((PartitionObject) o).getPartition();
092: } else {
093: partition = calculatePartition();
094: }
095: _pools[partition].releaseObject(o);
096: if (poolStatistic != null) {
097: poolStatistic.setCurrent(getLeased());
098: }
099: }
100:
101: public int getLeased() {
102: int leased = 0;
103: for (int i = 0; i < _pools.length; i++) {
104: leased += _pools[i].getLeased();
105: }
106: return leased;
107: }
108:
109: public int getMinSize() {
110: return _minSize;
111: }
112:
113: public int getMaxSize() {
114: return _maxSize;
115: }
116:
117: public long size() {
118: long result = 0;
119: for (int i = 0; i < _pools.length; i++) {
120: result += _pools[i].size();
121: }
122: return result;
123: }
124:
125: public int[] getPartitionUse() {
126: return (int[]) _partitionUse.clone();
127: }
128:
129: public void setMaxSize(int maxSize) {
130: synchronized (this ) {
131: if (_destroyed) {
132: throw new IllegalStateException();
133: }
134: int partitionMaxSize = maxSize / _partitions;
135: int partitionMaxRest = maxSize % _partitions;
136: int max = partitionMaxSize
137: + (((partitionMaxRest--) > 0) ? 1 : 0);
138: for (int i = 0; i < _partitions; i++) {
139: _pools[i].setMaxSize(max);
140: max = partitionMaxSize
141: + (((partitionMaxRest--) > 0) ? 1 : 0);
142: }
143: }
144: }
145:
146: public void destroy() {
147: synchronized (this ) {
148: if (_destroyed) {
149: throw new IllegalStateException();
150: }
151: _destroyed = true;
152: for (int i = 0; i < _partitions; i++) {
153: _pools[i].destroy();
154: }
155: }
156: }
157:
158: public final boolean doesReuseObjects() {
159: return true;
160: }
161:
162: protected void finalize() {
163: if (!_destroyed) {
164: destroy();
165: }
166: }
167:
168: public void setPoolStatistic(PoolStatistic poolStatistic) {
169: this .poolStatistic = poolStatistic;
170: }
171:
172: private PoolStatistic poolStatistic;
173: }
|