Java多线程(1)

我回来了,兄弟们,以后稳定每日一更哈。废话不多说,直接上码。

 1 class MyThread extends Thread {
 2     private String who;
 3     public MyThread(String str) {
 4         who=str;
 5     }
 6     public void run() {   //线程执行体   
 7         for(int i=0;i<=5;i++) {
 8             try {
 9                 sleep((int)(1000*Math.random()));
10             }
11             catch(InterruptedException e) {
12                 System.out.println(e.toString());
13             }
14             System.out.println(who+"正在运行!!");
15         }
16     }
17 }
18 public class Test {
19 
20     public static void main(String[] args) {
21 
22         MyThread you=new MyThread("你");
23         MyThread she=new MyThread("她");
24         you.start();
25         she.start();
26         System.out.println("主方法main()运行结束");
27         
28     }
29 
30 }

还是老规矩,难度依次上升。有什么不足之处下方评论,你的支持,是我更新最大的动力。

posted @ 2021-04-12 16:13  李家宇  阅读(35)  评论(0编辑  收藏  举报