本帖最后由 DOOR 于 2014-1-4 18:03 编辑
我这样做也可以实现四个窗口同时卖100张票啊,为什么要过渡到Runnable接口?这样做到底有什么不妥的地方,求解。。。。是不是因为,Thread本类对象已经在主线程中实现了开启线程动作,所以就不再需要Ticket去做他的子类?
Runnable出现的意义只是为了帮Thread本类对象去装start()后的run()方法?
- class Ticket extends Thread// implements Runnable
- {
- private int t=100;
-
- public void run()
- {
- while(true)
- {
- if (t>0)
- System.out.println(Thread.currentThread().getName()+"出票号码------"+t--);
- }
- }
- }
- class Hello
- {
- public static void main(String[] arg)
- {
- Ticket t = new Ticket();
- Thread t1 = new Thread(t);
- Thread t2 = new Thread(t);
- Thread t3 = new Thread(t);
- Thread t4 = new Thread(t);
- //Ticket t2 = new Ticket();
- //Ticket t3 = new Ticket();
- //Ticket t4 = new Ticket();
- t1.start();
- t2.start();
- t3.start();
- t4.start();
- }
- }
复制代码
|