// thread.ii /** * The expected use of the thread library is: * 1. Create a class that implements Runnable, putting * the code you want to execute in the run() method * 2. Call createThread(), passing it an instance of your * Runnable class. * 3. Possibly perform extra initialization work, then * invoke start() on the Thread that createThread() returned. * 4. Once the initial (main) thread terminates, all other threads * will be terminated. So don't allow the main thread to * terminate before any others if you want them to complete * execution. Use a synchronization object or the wait() * function to accomplish this. */ /** * This interface should be implemented by any class * whose instances are to be executed by a thread. */ interface Runnable { /** * Thread execution will begin with this method. * Thread execution will end when this method returns. */ run() } /** * A thread, or lightweight process. */ interface Thread { /** * Begin execution of this thread. * May only be called once. * Subsequent calls will have no effect. */ start() /** * Get the thread identifier for this thread. * See getCurrentThreadId(). */ getId():int /** * Return the priority level of this thread. * Priority levels are enumerated below. */ getPriority():int /** * Set the priority level of this thread. * Priority levels are enumerated below. */ setPriority(priority:int) } /** * Create a thread that will run the given Runnable object. * Precondition: r is not null * Postcondition: A thread is returned, but it is suspended. * Use Thread.start() to begin its execution. * If an error occurs in creation, null is returned. */ createThread(r:Runnable):Thread /** * Return the thread identifier of the currently executing thread. * A thread identifier is an integer that uniquely identifies * a thread while it is executing. */ getCurrentThreadId():int /** * Cause the currently executing thread to sleep for * _millis_ milliseconds. * Precondition: millis > 0 */ sleep(millis:int) /** * Cause the currently executing thread to sleep until * the given thread terminates. Warning: if t never * terminates, this will never return! * Precondition: t is not null */ wait(t:Thread) /** * Yield the remainder of the currently executing thread's * timeslice. */ yield() // These are the legal priority levels for threads. THREAD_PRIORITY_IDLE = 1 THREAD_PRIORITY_LOW = 2 THREAD_PRIORITY_BELOW_NORMAL = 3 THREAD_PRIORITY_NORMAL = 4 THREAD_PRIORITY_ABOVE_NORMAL = 5 THREAD_PRIORITY_HIGH = 6 THREAD_PRIORITY_CRITICAL = 7