001: /*
002: * $Id: AnyThreadPool.java,v 1.7 2002/09/16 08:05:03 jkl Exp $
003: *
004: * Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
005: *
006: * Use is subject to license terms, as defined in
007: * Anvil Sofware License, Version 1.1. See LICENSE
008: * file, or http://njet.org/license-1.1.txt
009: */
010: package anvil.core.runtime;
011:
012: import anvil.java.lang.ThreadPool;
013: import anvil.core.Any;
014: import anvil.core.AnyAbstractClass;
015: import anvil.script.Context;
016: import anvil.script.Function;
017:
018: /// @class ThreadPool
019: /// ThreadPool is pool of re-usable threads.
020: /// Re-use of threads is beneficial as the relative heavy-weight
021: /// creation of thread can be avoided.
022:
023: /**
024: * class AnyThreadPool
025: *
026: * @author: Jani Lehtimäki
027: */
028: public class AnyThreadPool extends AnyAbstractClass {
029:
030: public static final anvil.core.RuntimePermission CAN_USE = new anvil.core.RuntimePermission(
031: "anvil.runtime.ThreadPool", false);
032:
033: /// @constructor ThreadPool
034: /// Creates new thread pool.
035: /// @synopsis ThreadPool()
036: /// @synopsis ThreadPool(string name)
037: /// @synopsis ThreadPool(string name, int maxthreads)
038: /// @synopsis ThreadPool(string name, int maxthreads, int maxtasks)
039: public static final Object[] newInstance = new Object[] { null,
040: "*name", null, "*threads", new Integer(0), "*tasks",
041: new Integer(0) };
042:
043: public static final Any newInstance(Context context, String name,
044: int threads, int tasks) {
045: context.checkAccess(AnyThreadPool.CAN_USE);
046: if (name == null) {
047: name = "Pool";
048: }
049: ThreadPool pool = new ThreadPool(name);
050: if (threads > 0) {
051: pool.setMaxThreads(threads);
052: }
053: if (tasks > 0) {
054: pool.setMaxTasks(tasks);
055: }
056: return new AnyThreadPool(pool);
057: }
058:
059: private ThreadPool _pool;
060:
061: public AnyThreadPool(ThreadPool pool) {
062: _pool = pool;
063: }
064:
065: public final anvil.script.ClassType classOf() {
066: return __class__;
067: }
068:
069: public Object toObject() {
070: return _pool;
071: }
072:
073: /// @method getMaxTasks
074: /// Gets the maximum number of tasks. Sum of free, active and waiting
075: /// tasks never exceeds this number.
076: /// @synopsis int getMaxTasks()
077: public Any m_getMaxTasks() {
078: return Any.create(_pool.getMaxTasks());
079: }
080:
081: /// @method setMaxTasks
082: /// Gets the maximum number of tasks. Sum of free, active and waiting
083: /// tasks never exceeds this number.
084: /// @synopsis ThreadPool setMaxTasks(int maxtask)
085: public static final Object[] p_setMaxTasks = { "maxTasks" };
086:
087: public Any m_setMaxTasks(int max) {
088: if (max < 2) {
089: max = 2;
090: }
091: _pool.setMaxTasks(max);
092: return this ;
093: }
094:
095: /// @method getMaxThreads
096: /// Gets the maximum number of threads. No more that the given
097: /// number of threads are created.
098: /// @synopsis int getMaxThreads()
099: public Any m_getMaxThreads() {
100: return Any.create(_pool.getMaxThreads());
101: }
102:
103: /// @method setMaxThreads
104: /// Sets the maximum number of threads. No more that the given
105: /// number of threads are created.
106: /// @synopsis ThreadPool setMaxThreads(int maxthreads)
107: public static final Object[] p_setMaxThreads = { "maxThreads" };
108:
109: public Any m_setMaxThreads(int max) {
110: if (max < 2) {
111: max = 2;
112: }
113: _pool.setMaxThreads(max);
114: return this ;
115: }
116:
117: /// @method threads
118: /// Gets the number of threads created.
119: /// @synopsis int threads()
120: public Any m_threads() {
121: return Any.create(_pool.threads());
122: }
123:
124: /// @method waiting
125: /// Gets the number of tasks waiting to be executed.
126: /// @synopsis int waiting()
127: public Any m_waiting() {
128: return Any.create(_pool.waiting());
129: }
130:
131: /// @method shutdown
132: /// Shuts down this thread pool. All threads are closed but
133: /// the pool remains usable after shutdown.
134: /// @synopsis ThreadPool shutdown()
135: public Any m_shutdown() {
136: _pool.shutdown();
137: return this ;
138: }
139:
140: /// @method spawn
141: /// Spawns a task. Task may not be immediately executed if
142: /// all threads are currently in use.
143: /// @synopsis ThreadPool spawn(object callable, ..parameters)
144: public static final Object[] p_spawn = { null, "callable",
145: "parameters" };
146:
147: public Any m_spawn(Context context, Any callable, Any[] parameters) {
148: FunctionTask task = new FunctionTask(context, callable,
149: parameters);
150: _pool.spawn(task);
151: return this ;
152: }
153:
154: public static final anvil.script.compiler.NativeClass __class__ = new anvil.script.compiler.NativeClass(
155: "ThreadPool",
156: AnyThreadPool.class,
157: //DOC{{
158: ""
159: + " @class ThreadPool\n"
160: + " ThreadPool is pool of re-usable threads. \n"
161: + " Re-use of threads is beneficial as the relative heavy-weight \n"
162: + " creation of thread can be avoided.\n"
163: + " @constructor ThreadPool\n"
164: + " Creates new thread pool.\n"
165: + " @synopsis ThreadPool()\n"
166: + " @synopsis ThreadPool(string name)\n"
167: + " @synopsis ThreadPool(string name, int maxthreads)\n"
168: + " @synopsis ThreadPool(string name, int maxthreads, int maxtasks)\n"
169: + " @method getMaxTasks\n"
170: + " Gets the maximum number of tasks. Sum of free, active and waiting\n"
171: + " tasks never exceeds this number.\n"
172: + " @synopsis int getMaxTasks()\n"
173: + " @method setMaxTasks\n"
174: + " Gets the maximum number of tasks. Sum of free, active and waiting\n"
175: + " tasks never exceeds this number.\n"
176: + " @synopsis ThreadPool setMaxTasks(int maxtask)\n"
177: + " @method getMaxThreads\n"
178: + " Gets the maximum number of threads. No more that the given\n"
179: + " number of threads are created.\n"
180: + " @synopsis int getMaxThreads()\n"
181: + " @method setMaxThreads\n"
182: + " Sets the maximum number of threads. No more that the given\n"
183: + " number of threads are created.\n"
184: + " @synopsis ThreadPool setMaxThreads(int maxthreads)\n"
185: + " @method threads\n"
186: + " Gets the number of threads created.\n"
187: + " @synopsis int threads()\n"
188: + " @method waiting\n"
189: + " Gets the number of tasks waiting to be executed.\n"
190: + " @synopsis int waiting()\n"
191: + " @method shutdown\n"
192: + " Shuts down this thread pool. All threads are closed but\n"
193: + " the pool remains usable after shutdown.\n"
194: + " @synopsis ThreadPool shutdown()\n"
195: + " @method spawn\n"
196: + " Spawns a task. Task may not be immediately executed if\n"
197: + " all threads are currently in use.\n"
198: + " @synopsis ThreadPool spawn(object callable, ..parameters)\n"
199: //}}DOC
200: );
201: static {
202: RuntimeModule.class.getName();
203: }
204:
205: }
|