/*
* $Id: AnyThreadPool.java,v 1.7 2002/09/16 08:05:03 jkl Exp $
*
* Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
*
* Use is subject to license terms, as defined in
* Anvil Sofware License, Version 1.1. See LICENSE
* file, or http://njet.org/license-1.1.txt
*/
package anvil.core.runtime;
import anvil.java.lang.ThreadPool;
import anvil.core.Any;
import anvil.core.AnyAbstractClass;
import anvil.script.Context;
import anvil.script.Function;
/// @class ThreadPool
/// ThreadPool is pool of re-usable threads.
/// Re-use of threads is beneficial as the relative heavy-weight
/// creation of thread can be avoided.
/**
* class AnyThreadPool
*
* @author: Jani Lehtim�ki
*/
public class AnyThreadPool extends AnyAbstractClass
{
public static final anvil.core.RuntimePermission CAN_USE = new anvil.core.RuntimePermission("anvil.runtime.ThreadPool", false);
/// @constructor ThreadPool
/// Creates new thread pool.
/// @synopsis ThreadPool()
/// @synopsis ThreadPool(string name)
/// @synopsis ThreadPool(string name, int maxthreads)
/// @synopsis ThreadPool(string name, int maxthreads, int maxtasks)
public static final Object[] newInstance = new Object[] { null, "*name", null, "*threads", new Integer(0), "*tasks", new Integer(0) };
public static final Any newInstance(Context context, String name, int threads, int tasks)
{
context.checkAccess(AnyThreadPool.CAN_USE);
if (name == null) {
name = "Pool";
}
ThreadPool pool = new ThreadPool(name);
if (threads > 0) {
pool.setMaxThreads(threads);
}
if (tasks > 0) {
pool.setMaxTasks(tasks);
}
return new AnyThreadPool(pool);
}
private ThreadPool _pool;
public AnyThreadPool(ThreadPool pool)
{
_pool = pool;
}
public final anvil.script.ClassType classOf() {
return __class__;
}
public Object toObject()
{
return _pool;
}
/// @method getMaxTasks
/// Gets the maximum number of tasks. Sum of free, active and waiting
/// tasks never exceeds this number.
/// @synopsis int getMaxTasks()
public Any m_getMaxTasks()
{
return Any.create(_pool.getMaxTasks());
}
/// @method setMaxTasks
/// Gets the maximum number of tasks. Sum of free, active and waiting
/// tasks never exceeds this number.
/// @synopsis ThreadPool setMaxTasks(int maxtask)
public static final Object[] p_setMaxTasks = { "maxTasks" };
public Any m_setMaxTasks(int max)
{
if (max<2) {
max = 2;
}
_pool.setMaxTasks(max);
return this;
}
/// @method getMaxThreads
/// Gets the maximum number of threads. No more that the given
/// number of threads are created.
/// @synopsis int getMaxThreads()
public Any m_getMaxThreads()
{
return Any.create(_pool.getMaxThreads());
}
/// @method setMaxThreads
/// Sets the maximum number of threads. No more that the given
/// number of threads are created.
/// @synopsis ThreadPool setMaxThreads(int maxthreads)
public static final Object[] p_setMaxThreads = { "maxThreads" };
public Any m_setMaxThreads(int max)
{
if (max<2) {
max = 2;
}
_pool.setMaxThreads(max);
return this;
}
/// @method threads
/// Gets the number of threads created.
/// @synopsis int threads()
public Any m_threads()
{
return Any.create(_pool.threads());
}
/// @method waiting
/// Gets the number of tasks waiting to be executed.
/// @synopsis int waiting()
public Any m_waiting()
{
return Any.create(_pool.waiting());
}
/// @method shutdown
/// Shuts down this thread pool. All threads are closed but
/// the pool remains usable after shutdown.
/// @synopsis ThreadPool shutdown()
public Any m_shutdown()
{
_pool.shutdown();
return this;
}
/// @method spawn
/// Spawns a task. Task may not be immediately executed if
/// all threads are currently in use.
/// @synopsis ThreadPool spawn(object callable, ..parameters)
public static final Object[] p_spawn = { null, "callable", "parameters" };
public Any m_spawn(Context context, Any callable, Any[] parameters)
{
FunctionTask task = new FunctionTask(context, callable, parameters);
_pool.spawn(task);
return this;
}
public static final anvil.script.compiler.NativeClass __class__ =
new anvil.script.compiler.NativeClass("ThreadPool", AnyThreadPool.class,
//DOC{{
""+
" @class ThreadPool\n" +
" ThreadPool is pool of re-usable threads. \n" +
" Re-use of threads is beneficial as the relative heavy-weight \n" +
" creation of thread can be avoided.\n" +
" @constructor ThreadPool\n" +
" Creates new thread pool.\n" +
" @synopsis ThreadPool()\n" +
" @synopsis ThreadPool(string name)\n" +
" @synopsis ThreadPool(string name, int maxthreads)\n" +
" @synopsis ThreadPool(string name, int maxthreads, int maxtasks)\n" +
" @method getMaxTasks\n" +
" Gets the maximum number of tasks. Sum of free, active and waiting\n" +
" tasks never exceeds this number.\n" +
" @synopsis int getMaxTasks()\n" +
" @method setMaxTasks\n" +
" Gets the maximum number of tasks. Sum of free, active and waiting\n" +
" tasks never exceeds this number.\n" +
" @synopsis ThreadPool setMaxTasks(int maxtask)\n" +
" @method getMaxThreads\n" +
" Gets the maximum number of threads. No more that the given\n" +
" number of threads are created.\n" +
" @synopsis int getMaxThreads()\n" +
" @method setMaxThreads\n" +
" Sets the maximum number of threads. No more that the given\n" +
" number of threads are created.\n" +
" @synopsis ThreadPool setMaxThreads(int maxthreads)\n" +
" @method threads\n" +
" Gets the number of threads created.\n" +
" @synopsis int threads()\n" +
" @method waiting\n" +
" Gets the number of tasks waiting to be executed.\n" +
" @synopsis int waiting()\n" +
" @method shutdown\n" +
" Shuts down this thread pool. All threads are closed but\n" +
" the pool remains usable after shutdown.\n" +
" @synopsis ThreadPool shutdown()\n" +
" @method spawn\n" +
" Spawns a task. Task may not be immediately executed if\n" +
" all threads are currently in use.\n" +
" @synopsis ThreadPool spawn(object callable, ..parameters)\n"
//}}DOC
);
static {
RuntimeModule.class.getName();
}
}