01: // ***************************************************************
02: // * *
03: // * File: ThreadPool.java *
04: // * *
05: // * Copyright (c) 2002 Sun Microsystems, Inc. *
06: // * All rights reserved. *
07: // * *
08: // * *
09: // * Author - alejandro.abdelnur@sun.com *
10: // * *
11: // ***************************************************************
12:
13: package com.sun.portal.common.pool;
14:
15: public class ThreadPool {
16:
17: private static class ThreadManager implements ObjectManager {
18: private ThreadPool _threadPool;
19:
20: private ThreadManager(ThreadPool threadPool) {
21: _threadPool = threadPool;
22: }
23:
24: public Object createObject(Object param) {
25: return new ReusableThread(_threadPool);
26: }
27:
28: public void destroyObject(Object o) {
29: ((ReusableThread) o).finish();
30: }
31: }
32:
33: private ObjectPool _pool;
34:
35: public ThreadPool(int minSize, int maxSize, boolean overflow,
36: int partitions) {
37: _pool = new ObjectPool(new ThreadManager(this ), minSize,
38: maxSize, overflow, partitions);
39: }
40:
41: // when the runnable ends the thread is automatically returned to the pool
42: public ReusableThread obtainThread(Runnable runnable) {
43: if (runnable == null) {
44: throw new IllegalArgumentException(
45: "ThreadPool.obtainThread() - runnable can not be null");
46: }
47: ReusableThread rt = (ReusableThread) _pool.getPool()
48: .obtainObject(null);
49: if (rt != null) {
50: rt.setRunnable(runnable);
51: rt.setReuseThread(_pool.getPool().doesReuseObjects());
52: }
53: return rt;
54: }
55:
56: void releaseThread(ReusableThread thread) {
57: _pool.getPool().releaseObject(thread);
58: }
59:
60: public int getLeased() {
61: return _pool.getPool().getLeased();
62: }
63:
64: public int getMinSize() {
65: return _pool.getPool().getMinSize();
66: }
67:
68: public int getMaxSize() {
69: return _pool.getPool().getMaxSize();
70: }
71:
72: public void setMaxSize(int maxSize) {
73: _pool.getPool().setMaxSize(maxSize);
74: }
75:
76: public void destroy() {
77: _pool.getPool().destroy();
78: }
79:
80: public int[] getPartitionUse() {
81: return (_pool.getPool() instanceof ParallelObjectPool) ? ((ParallelObjectPool) _pool
82: .getPool()).getPartitionUse()
83: : null;
84: }
85:
86: }
|