01: package org.methodize.nntprss.util;
02:
03: /* -----------------------------------------------------------
04: * nntp//rss - a bridge between the RSS world and NNTP clients
05: * Copyright (c) 2002, 2003 Jason Brome. All Rights Reserved.
06: *
07: * email: nntprss@methodize.org
08: * mail: Methodize Solutions
09: * PO Box 3865
10: * Grand Central Station
11: * New York NY 10163
12: *
13: * This file is part of nntp//rss
14: *
15: * nntp//rss is free software; you can redistribute it
16: * and/or modify it under the terms of the GNU General
17: * Public License as published by the Free Software Foundation;
18: * either version 2 of the License, or (at your option) any
19: * later version.
20: *
21: * This program is distributed in the hope that it will be
22: * useful, but WITHOUT ANY WARRANTY; without even the implied
23: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
24: * PURPOSE. See the GNU General Public License for more
25: * details.
26: *
27: * You should have received a copy of the GNU General Public
28: * License along with this program; if not, write to the
29: * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
30: * Boston, MA 02111-1307 USA
31: * ----------------------------------------------------- */
32:
33: import java.util.Stack;
34:
35: /**
36: * @author Jason Brome <jason@methodize.org>
37: * @version $Id: SimpleThreadPool.java,v 1.3 2003/03/22 16:35:23 jasonbrome Exp $
38: */
39: public class SimpleThreadPool {
40:
41: //TODO: implement clean shutdown
42:
43: private Stack threadStack = new Stack();
44: private ThreadGroup threadGroup = null;
45: private String threadName;
46: //private int threadCount = 0;
47: private int maxThreads;
48:
49: public SimpleThreadPool(String name, String threadName,
50: int maxThreads) {
51: // Thread groups enable easier debugging in certain IDE,
52: // so lets assign our pool threads to a specific group
53:
54: if (name != null) {
55: threadGroup = new ThreadGroup(name);
56: } else {
57: threadGroup = new ThreadGroup("anonymous");
58: }
59:
60: if (threadName != null) {
61: this .threadName = threadName;
62: } else {
63: this .threadName = "STP-anonymous";
64: }
65:
66: this .maxThreads = maxThreads;
67: }
68:
69: public synchronized void run(Runnable obj) {
70: WorkerThread worker = null;
71: // FIXME - Add pool limits
72: if (threadStack.size() == 0) {
73: worker = new WorkerThread(threadGroup, this , threadName);
74: worker.run(obj);
75: worker.start();
76: //threadCount++;
77: } else {
78: worker = (WorkerThread) threadStack.pop();
79: worker.run(obj);
80: }
81: }
82:
83: public synchronized void makeThreadAvailable(WorkerThread thread) {
84: if (threadStack.size() > maxThreads) {
85: thread.end();
86: } else {
87: threadStack.push(thread);
88: }
89: }
90:
91: }
|