通过实现Runnable接口来实现多线程

 1 package testBlog;
 2 
 3 class MyThread implements Runnable{//这种线程创建方式可以避免单继承局限
 4     String name;
 5     
 6     MyThread(String name){
 7         this.name = name;
 8     }//用构造器给线程起名
 9     
10     public void run() {//这里要记得覆写run()方法
11         for(int x = 0;x<20;x++) {
12             System.out.println(this.name+"--->"+x);
13         }
14     }
15     
16 }
17 public class Test{
18     public static void main(String[] args) {
19         MyThread mt1 = new MyThread("线程A");
20         MyThread mt2 = new MyThread("线程B");
21         MyThread mt3 = new MyThread("线程C");
22         
23         //Runnable接口只能用run()方法来启动,但可以通过Thread类匿名对象的形式,来使用start()方法
24         mt1.run();
25         (new Thread(mt2)).start();
26         (new Thread(mt3)).start();
27         
28     }
29 }
原文地址:https://www.cnblogs.com/ssC2H4/p/8186334.html