Thread Notes
Thread Notes
2/8/2001 1 2/8/2001 2
1
Runnable Classes Using Runnable
• There are many situations where we want to • This class executes one of its methods in a
execute a computation concurrently, but in a class separate thread
that’s not a subclass of Thread.
class FooBar implements Runnable {
• We still need a Thread object to create and public void foo( ) {
control the thread. for (int i=0; i<100; i++)
System.out.println(“foo ”);
• A thread can begin execution in any class that }
implements Runnable and contains a run
method. public void bar( ) {
for (int i=0; i<100; i++)
public interface Runnable { System.out.println(“bar ”);
public abstract void run(); }
} ...
2/8/2001 7 2/8/2001 8