01: /**
02: * $Id: ThreadPool.java,v 1.4 2002/06/20 22:54:31 cathywu Exp $
03: * Copyright 2002 Sun Microsystems, Inc. All
04: * rights reserved. Use of this product is subject
05: * to license terms. Federal Acquisitions:
06: * Commercial Software -- Government Users
07: * Subject to Standard License Terms and
08: * Conditions.
09: *
10: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
11: * are trademarks or registered trademarks of Sun Microsystems,
12: * Inc. in the United States and other countries.
13: */package com.sun.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: }
|