001: /*
002: * $Id: GWThreadPool.java,v 1.9 2005/12/09 10:38:17 ss150821 Exp $
003: * $Source: /m/portal/ps/srap/src/com/sun/portal/util/GWThreadPool.java,v $
004: * $Log: GWThreadPool.java,v $
005: * Revision 1.9 2005/12/09 10:38:17 ss150821
006: * 4925306
007: *
008: * Revision 1.8 2005/11/30 05:57:43 ss150821
009: * 4925306
010: *
011: * Revision 1.7 2005/03/18 06:41:46 np145014
012: * CR 6223492
013: *
014: * Revision 1.6 2005/03/01 10:36:06 np145014
015: * CR 6224556
016: *
017: * Revision 1.5 2005/02/23 11:39:13 ss150821
018: * RFE 6223490 - SRA Should use JDK based logging
019: *
020: * Revision 1.4 2005/02/21 08:03:16 ss150821
021: * 6223490 - JDK Logging move for SRA
022: *
023: * Revision 1.3 2005/02/17 08:27:38 ss150821
024: * RFE 6223490 - Add JDK Logging Support to SRA
025: *
026: * Revision 1.2 2002/06/21 13:04:15 bv131302
027: * LDAP Attribute name changes
028: *
029: * Revision 1.1 2002/06/14 09:04:23 rt130506
030: * SRAP rebranding
031: *
032: * Revision 1.3 2002/06/11 16:02:12 bv131302
033: * new branded
034: *
035: * Revision 1.2 2002/05/13 06:22:26 mm132998
036: * Perf related modifications
037: *
038: */
039: /*
040: * @(#)GWThreadPool.java 1.2 00/11/15
041: *
042: * Copyright (c) 11/15/00 Sun Microsystems, Inc. All Rights Reserved.
043: */
044:
045: package com.sun.portal.util;
046:
047: import com.sun.portal.rproxy.configservlet.client.GatewayProfile;
048:
049: public class GWThreadPool {
050:
051: private static int threadPoolSize;
052:
053: private static _ThreadPool threadPool;
054: protected final static ThreadGroup threadGroup = new ThreadGroup(
055: "GW ThreadPool Group");
056:
057: public static void init() {
058: threadPoolSize = GatewayProfile
059: .getInt("MaxThreadPoolSize", 500);
060:
061: threadPool = new _ThreadPool(threadPoolSize);
062: }
063:
064: public static void run(Runnable task) throws InterruptedException {
065: _ThreadPool.poolMon.poolThreadStart();
066: threadPool.run(task);
067: }
068:
069: public static int getSize() {
070: return threadPoolSize;
071: }
072:
073: public static int getRequestsInQue() {
074: return threadPool.getSize();
075: }
076:
077: public static int getActiveThreads() {
078: //return threadGroup.activeCount();
079: //return threadGroup.enumerate(new Thread[threadGroup.activeCount()]);
080: return threadPool.poolMon.getActiveThreads();
081: }
082:
083: }
084:
085: class Que implements java.io.Serializable {
086:
087: private java.util.List list = new java.util.ArrayList();
088:
089: synchronized public void put(Runnable obj) {
090: TaskWrapper task = (TaskWrapper) TaskWrapper.wrap(obj);
091: task.queued();
092: list.add(task);
093: notify();
094: }
095:
096: synchronized public Runnable get() {
097: while (list.isEmpty()) {
098: try {
099: wait();
100: } catch (InterruptedException ex) {
101: }
102: }
103: TaskWrapper taskWrapper = (TaskWrapper) list.remove(0);
104: taskWrapper.deQueued();
105:
106: return taskWrapper;
107: }
108:
109: synchronized public int size() {
110: return list.size();
111: }
112: }
113:
114: class _ThreadPool {
115: public static PoolMonitor poolMon = new PoolMonitor();
116: private Que tasks = new Que();
117:
118: public _ThreadPool(int threadCount) {
119: for (int i = 0; i < threadCount; i++) {
120: ThreadPoolThread thread = new ThreadPoolThread(this , i);
121: thread.start();
122: }
123: }
124:
125: public void run(Runnable task) {
126: tasks.put(task);
127: }
128:
129: public Runnable getNextRunnable() {
130: return (Runnable) tasks.get();
131: }
132:
133: public int getSize() {
134: return tasks.size();
135: }
136: }
137:
138: class ThreadPoolThread extends Thread {
139:
140: private _ThreadPool pool;
141:
142: public ThreadPoolThread(_ThreadPool pool, int id) {
143: super (GWThreadPool.threadGroup, "ID_" + id);
144: this .pool = pool;
145: }
146:
147: public void run() {
148: ThreadMonitorContext.startThread();
149:
150: try {
151: while (true) {
152: ThreadMonitorContext.waitingInQueue();
153: Runnable task = pool.getNextRunnable();
154: if (task != null) {
155: try {
156: ThreadMonitorContext.newTask(task);
157: task.run();
158: ThreadMonitorContext.taskEnds();
159: } catch (Throwable ex) {
160: ThreadMonitorContext.taskEnds();
161: if (GWDebug.debug.messageEnabled()) {
162: GWDebug.debug.message(
163: "Threadpool caught Exception : ",
164: ex);
165: }
166: }
167: }
168: }
169: } finally {
170: pool.poolMon.poolThreadEnd();
171: ThreadMonitorContext.endThread();
172: }
173: }
174: }
175:
176: class PoolMonitor {
177: boolean pool_started = false;
178:
179: int activeThreads = 0;
180:
181: synchronized void poolThreadStart() {
182: activeThreads++;
183: pool_started = true;
184: }
185:
186: synchronized void poolThreadEnd() {
187: activeThreads--;
188: }
189:
190: synchronized int getActiveThreads() {
191: return activeThreads;
192: }
193: }
|