Name

Thread

Synopsis

A class that represents a thread of execution in the Java virtual machine. Each thread has its own call stack and its own copy of local variables created by the Java methods that it executes.

An application may create a new thread of execution by instantiating a subclass of Thread and overriding the run() method, or by implementing a Runnable interface and passing its reference to the Thread constructor. A new thread is created in an inactive state. To begin execution, its start() method must be invoked. The total number of threads in existence can be obtained by calling the static activeCount() method.

Once a thread is running, it continues to do so until the run() of the Thread subclass terminates, or the Runnable returns control to its caller. Note that even though its thread of execution has ended, a Thread object continues to exist until all references to it have been released and the object is removed by the garbage collector. The isAlive() method can be used to determine whether a Thread object still has an active thread of execution.

Unlike J2SE, the CLDC Thread class does not provide the stop(), suspend() and resume() methods. However, there are two Thread methods that allow a thread to suspend its own execution. The static sleep() method suspends the thread for a fixed period of time specified in milliseconds. The thread will be scheduled for resumption when the delay period expires, but may not resume immediately if another thread is currently running. ...

Get J2ME in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.