java:线程,多个线程作用于同一个runnable对象

View Code
 1 package com.chnfuture;
 2 
 3 public class MultiThread {
 4 
 5     /**
 6      * @param args
 7      * @throws InterruptedException 
 8      */
 9     public static void main(String[] args) throws Exception {
10         // TODO Auto-generated method stub
11         R r = new R();
12         new Thread().start();
13         for(int i=0;i<10;i++){
14             new Thread(r).start();
15         }
16     }
17 
18 }
19 
20 class MyThread extends Thread{
21     public int x = 0,i;
22     @Override
23     public void run() {
24         // TODO Auto-generated method stub
25         super.run();
26         for(i=0;i<100;i++){
27             try {
28                 Thread.sleep(10);
29             } catch (Exception e) {
30                 // TODO: handle exception
31             }
32             System.out.println(x++);
33         }
34     }
35 }
36 
37 class R implements Runnable{
38 
39     private int x = 0;
40     @Override
41     public void run() {
42         // TODO Auto-generated method stub
43         for(int i=0;i<100;i++){
44               try{
45                 Thread.sleep(10);
46               }catch(Exception e){}
47               System.out.println(x++);
48         }
49     }
50 }
posted @ 2012-08-10 16:32  ligang305  阅读(931)  评论(0编辑  收藏  举报