001: // ***************************************************************
002: // * *
003: // * File: ParallelObjectPool.java *
004: // * *
005: // * Copyright (c) 2002 Sun Microsystems, Inc. *
006: // * All rights reserved. *
007: // * *
008: // * *
009: // * Author - alejandro.abdelnur@sun.com *
010: // * *
011: // ***************************************************************
012:
013: package com.sun.portal.common.pool;
014:
015: import java.util.Random;
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 long[] _partitionUse;
024: private Random _random;
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 long[partitions];
054: _random = new Random();
055: }
056:
057: private int calculatePartition() {
058: return (_partitions == 1) ? 0 : (int) (Math.abs(_random
059: .nextInt()) % _partitions);
060: }
061:
062: public Object obtainObject(Object param) {
063: if (_destroyed) {
064: throw new IllegalStateException();
065: }
066: int partition = calculatePartition();
067: Pool pool = _pools[partition];
068: Object o = pool.obtainObject(param);
069: if (o == null) {
070: for (int i = 0; o == null && i < _partitions; i++) {
071: if (i != partition) {
072: pool = _pools[i];
073: o = pool.obtainObject(param);
074: if (o != null) {
075: partition = i;
076: }
077: }
078: }
079: }
080: if (o != null && (o instanceof PartitionObject)) {
081: ((PartitionObject) o).setPartition(partition);
082: _partitionUse[partition]++;
083: }
084: return o;
085: }
086:
087: public void releaseObject(Object o) {
088: int partition;
089: if (o instanceof PartitionObject) {
090: partition = ((PartitionObject) o).getPartition();
091: } else {
092: partition = calculatePartition();
093: }
094: _pools[partition].releaseObject(o);
095: }
096:
097: public int getLeased() {
098: int leased = 0;
099: for (int i = 0; i < _pools.length; i++) {
100: leased += _pools[i].getLeased();
101: }
102: return leased;
103: }
104:
105: public int getMinSize() {
106: return _minSize;
107: }
108:
109: public int getMaxSize() {
110: return _maxSize;
111: }
112:
113: public int[] getPartitionUse() {
114: return (int[]) _partitionUse.clone();
115: }
116:
117: public void setMaxSize(int maxSize) {
118: synchronized (this ) {
119: if (_destroyed) {
120: throw new IllegalStateException();
121: }
122: int partitionMaxSize = maxSize / _partitions;
123: int partitionMaxRest = maxSize % _partitions;
124: int max = partitionMaxSize
125: + (((partitionMaxRest--) > 0) ? 1 : 0);
126: for (int i = 0; i < _partitions; i++) {
127: _pools[i].setMaxSize(max);
128: max = partitionMaxSize
129: + (((partitionMaxRest--) > 0) ? 1 : 0);
130: }
131: }
132: }
133:
134: public void destroy() {
135: synchronized (this ) {
136: if (_destroyed) {
137: throw new IllegalStateException();
138: }
139: _destroyed = true;
140: for (int i = 0; i < _partitions; i++) {
141: _pools[i].destroy();
142: }
143: }
144: }
145:
146: public final boolean doesReuseObjects() {
147: return true;
148: }
149:
150: protected void finalize() {
151: if (!_destroyed) {
152: destroy();
153: }
154: }
155:
156: }
|