January 2012
Intermediate to advanced
282 pages
7h 4m
English
A Thread object, that is, an instance of the Thread class defined by Java, is a unit of execution with its own call stack. Applications can create additional threads easily, as shown in Listing 5–1. Of course, your application is free to create additional threads to perform some operations outside of the main thread; very often you will have to do exactly that to keep your application responsive.
Listing 5–1. Creating Two Threads
// the run() method can simply be overridden…
Thread thread1 = new Thread("cheese1") {
@Override
public void run() {
Log.i(”thread1”, "I like Munster");
}
};
// …or a Runnable object can be passed to the Thread constructor Thread thread2 = new Thread(new ...