第一步:建立线程类,并启动线程

 1 /**
 2  * 【1】实现Runnable接口,重写run方法。
 3  * 【2】run方法内的逻辑代码就是线程体
 4  * 【3】创建线程类的对象
 5  * 【4】创建线程类的代理对象
 6  * 【5】代理对象调用start(),启动线程
 7  * @author it-c-1201
 8  *
 9  */
10 public class MyThread implements Runnable{
11 
12     //线程体
13     public void run() {
14         for(int a=0;a<100;a++){
15             System.out.println("MyThread.run(线程体运行)"+a);
16         }
17         
18     }
19 
20     
21     public static void main(String[] args) {
22         //创建真实角色。线程体本身的对象
23         MyThread myThread=new MyThread();
24         //创建静态代理角色
25         Thread dailiMyThread=new Thread(myThread);
26         //启动线程
27         dailiMyThread.start();
28         
29         for(int a=0;a<100;a++){
30             System.out.println("MyThread.main(main方法)"+a);
31         }
32     }
33     
34     
35 }
View Code

相关文章:

  • 2021-12-24
  • 2022-01-07
  • 2022-12-23
  • 2021-08-16
  • 2021-11-01
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-22
  • 2021-06-06
  • 2021-09-25
相关资源
相似解决方案