本来ChePiao应该实现Runnable接口,我写错了继承了Thread类,没想到也能运行。
是不是因为Thread实现了Runnable所以继承他的类就也实现了Runnable接口呢???- public class Text {
- public static void main(String[] args) {
- ChePiao c1 = new ChePiao();
- Thread t1 = new Thread(c1);
- Thread t2 = new Thread(c1);
- Thread t3 = new Thread(c1);
- Thread t4 = new Thread(c1);
- t1.start();
- t2.start();
- t3.start();
- t4.start();
- }
- }
- class ChePiao extends Thread {
- private int num = 100;
-
- @Override
- public void run() {
- while(num>0){
- System.out.println(Thread.currentThread().getName()+"车票..."+ num--);
- }
- }
-
- }
复制代码 |