JAvaスレッド実装の2つの方法

5190 ワード

 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 }