Game Development Reference
In-Depth Information
2.5 Main Thread
Each app has at least one thread running when it is started. A thread is a concurrent unit
of execution. It has its own call stack for methods being invoked, their arguments and local
variables.
Public Methods
According to the Android Developer Forums, there are two ways to execute code in a new
thread. You can either subclass Thread and overriding its run() method, or construct a new
Thread and pass a Runnable to the constructor. In either case, the start() method must be
called to actually execute the new Thread.
The table 2.3 describes its public methods that are commonly used in programming.
Method
Description
start()
Starts the new Thread of execution. The run() method of the receiver
will
be
called
by
the
receiver
Thread
itself.
Throws
Illeg-
alThreadStateException if this thread has already started.
run()
Calls the run() method of the Runnable object the receiver holds. If no
Runnable is set, does nothing.
sleep()
Causes the thread which sent this message to sleep for the given interval
of time (given in milliseconds). The precision is not guaranteed - the
Thread may sleep more or less than requested. Throws InterruptedEx-
ception if interrupt() is called for this Thread while it's sleeping.
interrupt()
Posts an interrupt request to this Thread. The behavior depends on the
state of this Thread.
isAlive()
Returns true if the receiver has already been started and still runs code
(hasn't died yet). Returns false either if the receiver hasn't been started
yet or if it has already started and run to completion and died.
setPriority()
Sets the priority of this thread. If the requested priority is greater than
the parent thread group's getMaxPriority() , the group's maximum prior-
ity will be used instead.
stop()
This method was deprecated in API level 1.
destroy()
This method was deprecated in API level 1.
Search WWH ::




Custom Search