實現接口開啟線程(實現Runnable接口)
-
步驟
- 定義類實現Runnable接口
- 重寫run()方法
- 在測試類創建子類對象
- 創建線程對象把子類對象作為參數傳入構造方法
- 用線程對象調用start()方法開啟線程
//1.類實現Runnable接口 public class BBB implements Runnable { //2.重寫run方法,把代碼寫在run方法里面 @Override public void run() { for (int i = 0; i < 100; i++) { System.out.println("新線程" + i); } } }
public class Test02 { public static void main(String[] args) { //3.創建子類對象 BBB b = new BBB(); //4.創建線程對象傳入子類對象作為參數 Thread t = new Thread(b); //5.調用start()方法開啟線程 t.start();
}
}

浙公網安備 33010602011771號