Implementing the Runnable interface
The second way to create a thread is to use a class that implements java.lang.Runnable
. Here is an example of such a class that has almost exactly the same functionality as the MyThread
class:
class MyRunnable implements Runnable {
private String parameter, name;
public MyRunnable(String name) {
this.name = name;
}
public void run() {
while(!"exit".equals(parameter)){
System.out.println("thread " + this.name +
", parameter: " + parameter);
...