通過thread類創建線程?
?
1 package hello; 2 /** 3 * 創建兩個子線程,每個線程均輸出20次消息數字、“你好”、線程名 4 * @author L 5 * 6 */ 7 public class MyThread extends Thread{ 8 9 public void run() { 10 for(int i=0;i<20;i++) { 11 Thread t=Thread.currentThread(); 12 System.out.println((i+1)+".你好,來自線程"+t.getName()); 13 } 14 } 15 }
?
1 package hello; 2 3 public class Main { 4 public static void main(String[] args) { 5 MyThread mt=new MyThread(); 6 Thread t1=new Thread(mt); 7 Thread t2=new Thread(mt); 8 t1.start(); 9 t2.start(); 10 11 12 } 13 }
?