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 }