Create Thread Implementing Runnable
Previous | Home | Next |
The second way to specify what code a thread should run is by creating a class that implements java.lang.Runnable. The Runnable object can be executed by a Thread.
To have the run() method executed by a thread, pass an instance of MyRunnable to a Thread in its constructor. Here is how that is done:
public class MyRunnable implements Runnable {
public void run(){
System.out.println("MyRunnable is running");
}
}
Thread thread = new Thread(new MyRunnable());
thread.start();
Previous | Home | Next |