wblyuyang

博客园 首页 新随笔 联系 订阅 管理
 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 }

 

posted on 2012-04-25 16:57  wblyuyang  阅读(184)  评论(0编辑  收藏  举报