執行緒定義
class 類別名稱 implements Runnable {
public void run ( )
以其他執行緒執行處理;
}
}
Car 類別繼承抽象類別,同時也繼承介面 Runnalbe,在 main ()產生 Car 物件後,要記得產生一個 Thread 類別的物件
abstract class Vehicle {
abstract void show();
}
class Car3 extends Vehicle implements Runnable {
String name;
Car3(String name) {
this.name = name;
}
public void run() {
for (int i=0; i < 6; i++)
show();
}
public void show() {
System.out.println("執行 "+name+" 執行緒");
}
}
public class ThreadRunnable {
public static void main(String[] args) {
Car3 car1 = new Car3("汽車1");
Thread thread = new Thread(car1);
thread.start();
Car3 car2 = new Car3("汽車2");
thread = new Thread(car2);
thread.start();
}
}
執行結果
沒有留言:
張貼留言