1 //继承Thread类 2 public class MyThread extends Thread { 3 //重写run方法 4 public void run() { 5 for(int i = 0;i<20;i++) { 6 System.out.println("你好,来自线程"+Thread.currentThread().getName()); 7 } 8 } 9 }
1 //测试类 2 public class Test { 3 public static void main(String[] args) { 4 MyThread mythread = new MyThread(); 5 MyThread mythread1 = new MyThread(); 6 mythread.start();//启动线程 7 mythread1.start(); 8 } 9 }
运行结果: