001: /*
002: * $Id: AnyThread.java,v 1.21 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.core.Any;
013: import anvil.core.AnyAbstractClass;
014: import anvil.script.Context;
015: import anvil.script.Function;
016:
017: /// @class Thread
018: /// A thread is a thread of execution in a program.
019: /// Multiple threads of execution may be running
020: /// concurrently.
021:
022: /**
023: * class AnyThread
024: *
025: * @author: Jani Lehtimäki
026: */
027: public class AnyThread extends AnyAbstractClass {
028:
029: public static final anvil.core.RuntimePermission CAN_USE = new anvil.core.RuntimePermission(
030: "anvil.runtime.Thread", false);
031:
032: public static final Any MIN_PRIORITY = Any
033: .create(Thread.MIN_PRIORITY);
034: public static final Any NORMAL_PRIORITY = Any
035: .create(Thread.NORM_PRIORITY);
036: public static final Any MAX_PRIORITY = Any
037: .create(Thread.MAX_PRIORITY);
038:
039: private static int _threadCount = 0;
040:
041: /// @constructor Thread
042: /// Creates new thread.
043: /// @synopsis Thread()
044: /// @synopsis Thread(string name)
045: public static final Object[] newInstance = { null, "*name", null };
046:
047: public static final Any newInstance(Context context, String name) {
048: context.checkAccess(AnyThread.CAN_USE);
049: if (name == null) {
050: name = "Task-" + (++_threadCount);
051: }
052: return new AnyThread(new FunctionThread(context,
053: new FunctionTask(), name));
054: }
055:
056: private FunctionThread _thread;
057:
058: public AnyThread(FunctionThread thread) {
059: _thread = thread;
060: }
061:
062: public final anvil.script.ClassType classOf() {
063: return __class__;
064: }
065:
066: public Object toObject() {
067: return _thread;
068: }
069:
070: /// @method getName
071: /// Returns the name of this thread.
072: /// @synopsis string getName()
073: public Any m_getName() {
074: return Any.create(_thread.getName());
075: }
076:
077: /// @method getPriority
078: /// Returns the priority setting for this thread.
079: /// @synopsis int getPriority()
080: public Any m_getPriority() {
081: return Any.create(_thread.getPriority());
082: }
083:
084: /// @method setName
085: /// Sets the name of this thread.
086: /// @synopsis Thread setName(string name)
087: public static final Object[] p_setName = { "name" };
088:
089: public Any m_setName(String name) {
090: _thread.setName(name);
091: return this ;
092: }
093:
094: /// @method setPriority
095: /// Sets the priority for this thread.
096: /// @synopsis Thread setPriority(int priority)
097: public static final Object[] p_setPriority = { "priority" };
098:
099: public Any m_setPriority(int p) {
100: if (p < Thread.MIN_PRIORITY) {
101: p = Thread.MIN_PRIORITY;
102: } else if (p > Thread.MAX_PRIORITY) {
103: p = Thread.MAX_PRIORITY;
104: }
105: _thread.setPriority(p);
106: return this ;
107: }
108:
109: /// @method interrupt
110: /// Interrupts this thread, possibly generating
111: /// Interrupted, or InterruptedIO exception in currently
112: /// pending operation in this thread.
113: /// @synopsis Thread interrupt()
114: public Any m_interrupt() {
115: _thread.interrupt();
116: return this ;
117: }
118:
119: /// @method interrupted
120: /// Checks if this thread has been interrupted.
121: /// Interrupted status is cleared when this method is called.
122: /// @synopsis boolean interrupted()
123: public Any m_interrupted() {
124: return _thread.interrupted() ? TRUE : FALSE;
125: }
126:
127: /// @method isInterrupted
128: /// Checks if this thread has been interrupted.
129: /// Interrupted status is NOT cleared when this method is called.
130: /// @synopsis boolean isInterrupted()
131: public Any m_isInterrupted() {
132: return _thread.isInterrupted() ? TRUE : FALSE;
133: }
134:
135: /// @method isAlive
136: /// Checks if this thread is still alive.
137: /// @synopsis boolean isAlive()
138: public Any m_isAlive() {
139: return _thread.isAlive() ? TRUE : FALSE;
140: }
141:
142: /// @method isDaemon
143: /// Checks if this thread is marked as daemon.
144: /// @synopsis boolean isDaemon()
145: public Any m_isDaemon() {
146: return _thread.isDaemon() ? TRUE : FALSE;
147: }
148:
149: /// @method setDaemon
150: /// Sets the daemon status of this thread.
151: /// @synopsis Thread setDaemon(boolean isDaemon)
152: /// @throws BadState If this thread is active
153: public static final Object[] p_setDaemon = { null, "isDaemon" };
154:
155: public Any m_setDaemon(Context context, boolean daemon) {
156: try {
157: _thread.setDaemon(daemon);
158: return this ;
159: } catch (IllegalStateException e) {
160: throw context.BadState(e.getMessage());
161: }
162: }
163:
164: /// @method join
165: /// @synopsis Thread join() ;
166: /// Waits indefinitly for this thread to terminate.
167: /// @synopsis Thread join(int millis) ;
168: /// Waits at most specified milliseconds for this thread to terminate.
169: /// @synopsis Thread join(int millis, int nanos) ;
170: /// Waits at most specified milli- and nanoseconds for this thread to terminate.
171: /// @throws Interrupted if another thread has interrupted the current thread.
172: public static final Object[] p_join = { null, "*millis",
173: new Long(0), "*nanos", new Integer(0) };
174:
175: public Any m_join(Context context, long millis, int nanos) {
176: if (nanos < 0) {
177: nanos = 0;
178: }
179: if (millis < 0) {
180: millis = 0;
181: }
182: try {
183: _thread.join(millis, nanos);
184: return this ;
185: } catch (InterruptedException e) {
186: throw context.Interrupted(e.getMessage());
187: }
188: }
189:
190: /// @method start
191: /// Starts the execution of thread.
192: /// @synopsis Thread start(Function callable, ..parameters)
193: /// @throws BadState If thread is already started
194: public static final Object[] p_start = { null, "callable",
195: "parameters" };
196:
197: public Any m_start(Context context, Any callable, Any[] parameters) {
198: try {
199: _thread.start(callable, parameters);
200: return this ;
201: } catch (IllegalStateException e) {
202: throw context.BadState(e.getMessage());
203: }
204: }
205:
206: public static final anvil.script.compiler.NativeClass __class__ = new anvil.script.compiler.NativeClass(
207: "Thread",
208: AnyThread.class,
209: //DOC{{
210: ""
211: + " @class Thread\n"
212: + " A thread is a thread of execution in a program. \n"
213: + " Multiple threads of execution may be running\n"
214: + " concurrently. \n"
215: + " @constructor Thread\n"
216: + " Creates new thread.\n"
217: + " @synopsis Thread()\n"
218: + " @synopsis Thread(string name)\n"
219: + " @method getName\n"
220: + " Returns the name of this thread.\n"
221: + " @synopsis string getName()\n"
222: + " @method getPriority\n"
223: + " Returns the priority setting for this thread.\n"
224: + " @synopsis int getPriority()\n"
225: + " @method setName\n"
226: + " Sets the name of this thread.\n"
227: + " @synopsis Thread setName(string name)\n"
228: + " @method setPriority\n"
229: + " Sets the priority for this thread.\n"
230: + " @synopsis Thread setPriority(int priority)\n"
231: + " @method interrupt\n"
232: + " Interrupts this thread, possibly generating \n"
233: + " Interrupted, or InterruptedIO exception in currently \n"
234: + " pending operation in this thread.\n"
235: + " @synopsis Thread interrupt()\n"
236: + " @method interrupted\n"
237: + " Checks if this thread has been interrupted.\n"
238: + " Interrupted status is cleared when this method is called.\n"
239: + " @synopsis boolean interrupted()\n"
240: + " @method isInterrupted\n"
241: + " Checks if this thread has been interrupted.\n"
242: + " Interrupted status is NOT cleared when this method is called.\n"
243: + " @synopsis boolean isInterrupted()\n"
244: + " @method isAlive\n"
245: + " Checks if this thread is still alive.\n"
246: + " @synopsis boolean isAlive()\n"
247: + " @method isDaemon\n"
248: + " Checks if this thread is marked as daemon.\n"
249: + " @synopsis boolean isDaemon()\n"
250: + " @method setDaemon\n"
251: + " Sets the daemon status of this thread.\n"
252: + " @synopsis Thread setDaemon(boolean isDaemon)\n"
253: + " @throws BadState If this thread is active\n"
254: + " @method join\n"
255: + " @synopsis Thread join() ;\n"
256: + " Waits indefinitly for this thread to terminate.\n"
257: + " @synopsis Thread join(int millis) ;\n"
258: + " Waits at most specified milliseconds for this thread to terminate.\n"
259: + " @synopsis Thread join(int millis, int nanos) ;\n"
260: + " Waits at most specified milli- and nanoseconds for this thread to terminate.\n"
261: + " @throws Interrupted if another thread has interrupted the current thread. \n"
262: + " @method start\n"
263: + " Starts the execution of thread.\n"
264: + " @synopsis Thread start(Function callable, ..parameters)\n"
265: + " @throws BadState If thread is already started\n"
266: //}}DOC
267: );
268: static {
269: RuntimeModule.class.getName();
270: }
271:
272: }
|