创建多线程

创建一个子线程,完成1-100之间自然数的输出,同样的,主线程执行同样的操作
创建多线程的第一种方式:继承java.lang.Thread类
 1 class SubThread extends Thread{
 2     //2、重写父类Thread的run方法,方法内实现此子线程要完成的功能
 3     //子线程
 4     public void run(){
 5         for (int i = 0; i < 100; i++) {
 6             System.out.println(Thread.currentThread().getName() +":"+i);
 7         }
 8     }
 9 }
10 
11 public class  TestThread{
12     public static void main(String[] args) {
13         //3、创建子线程的实例
14         SubThread st1 = new SubThread();
15         SubThread st2 = new SubThread();
16         //4、调用线程的start(),启动此线程;调用相应的run()方法
17         st1.start();
18         st2.start();
19         //5、一个线程只能执行一次start();
20         //st1.start();
21 
22         //主线程
23         for (int i = 0; i < 100; i++) {
24             System.out.println(Thread.currentThread().getName() +":"+i);
25         }
26     }
27 
28 }
原文地址:https://www.cnblogs.com/zhyn-BeHard/p/6486674.html