多线程练习1:使用继承Thread类的方式创建线程
需求:
1 public class Test { 2 public static void main(String[] args) { 3 //创建线程对象 4 Mythread thread1 = new Mythread(); 5 Mythread thread2 = new Mythread(); 6 //启动线程 7 thread2.start(); 8 thread1.start(); 9 } 10 } 11 //使用继承Thread类的方式创建线程 12 class Mythread extends Thread{ 13 public void run(){ 14 for(int i=1;i<=20;i++){ 15 System.out.println(i+".你好,来自线程"+Thread.currentThread().getName()); 16 } 17 } 18 }
运行结果: