java初学者 加入小组

293个成员 64个话题 创建时间:2015-02-03

Thread Runnable

发表于2015-07-25 3816次查看

class B extends Thread{
 private int tar=5;
 public void run(){
  for(int i = 1;i<100;i++){
   if(tar>0){
    System.out.println("卖票  =" +tar--);
    }   
  }
 }
}
public class TestThread01 {

 public static void main(String[] args) {
  B a = new B();
  
  a.start();
  a.start();
  a.start();
  }
}

为什么Runnable接口可以这样写而Thread不行  Thread这样写也能只卖五张票啊,,,

1回复
  • 2楼 魔乐技术辅导 2016-05-18

    因为最终调用start方法,都是只能由Thread类或者Thread的子类进行调用,你这样写实际上只有一个线程,但是这个线程被启用了三次

发表回复
你还没有登录,请先 登录或 注册!