1、thread不能设置共享资源、runnable可以设置共享资源
2、代码风格与结构,ruannbale更好些
3、
 
public class TestThread extends Thread {
    private int count = 5;
    
    @Override
    public void run() {
          for(int i=0; i<10; i++) {
              if(this.count>0) {
                   System.out.println(Thread.currentThread().getName + "-----" +this.count--);
              }
          }
    }
    
    public static void main(String[] args) {
         TestThread test1 = new TestThread();
          test1.start();
 
         
         TestThread test2 = new TestThread();
          test2.start();
    }
}
 
=============
 
public class TestRunnable implements Runnable {
    private int count = 5;
 
    public TestRunnable() {
         
    }
 
    @Override
    public void run() {
          for(int i=0; i< 20; i++) {
              if(this.count>0) {
                   System.out.println(Thread.currentThread().getName() + "----" + this.count--);
              }
          }
    }
}
 
public class Test {
    public static void main(String[] args) {
          TestRunnable tr = new TestRunnable();
           new Thread(tr).start("A");
           new Thread(tr).start("B");   
    }
 
}
posted on 2014-10-14 15:54  小龙1号  阅读(194)  评论(0编辑  收藏  举报