1 //方式一
 2 public class TestThread1 {
 3     public static void main(String args[]) {
 4         Runner1 r = new Runner1();
 5         r.start();
 6         
 7         for(int i=0; i<100; i++) {
 8             System.out.println("Main Thread:------" + i);
 9         }
10     }
11 }
12 
13 class Runner1 extends Thread {
14     public void run() {
15         for(int i=0; i<100; i++) {
16             System.out.println("Thread:" + i);
17         }
18     }
19 }
20 
21 //能使用接口就不用从Thread类继承
22 //方式二
23 public class TestThread1 {
24     public static void main(String args[]) {
25         Runner1 r = new Runner1();
26         r.run();
27         Thread t = new Thread(r);
28         t.start();
29         
30         for(int i=0; i<50; i++) {
31             System.out.println("Main Thread:------" + i);
32         }
33     }
34 }
35 
36 class Runner1 implements Runnable {
37     public void run() {
38         for(int i=0; i<50; i++) {
39             System.out.println("Thread: " + i);
40         }
41     }
42 }

 

相关文章:

  • 2022-12-23
  • 2022-02-09
  • 2021-11-19
  • 2021-05-31
  • 2022-12-23
  • 2021-04-06
  • 2022-12-23
猜你喜欢
  • 2021-06-11
  • 2022-02-09
  • 2022-02-09
  • 2021-12-30
  • 2021-07-15
  • 2021-09-09
相关资源
相似解决方案