public class Test
{
public static void main(String[] args) throws Exception 
{
A a = new A();
a.start();
a.run();//为什么是主线程调用,是a调用自己的run方法啊
}
}
class A extends Thread
{

public void run()
{
for(int i=0; i<10; i++)
{
System.out.println(currentThread().getName()+":"+i);
try{
sleep(100);
}catch(Exception e){

}
}
}
}

解决方案 »

  1.   

    A a = new A();
            a.start();为什么要下面这一句?
            a.run();//为什么是主线程调用,是a调用自己的run方法啊
      

  2.   

        public synchronized void start() {
            if (threadStatus != 0 || this != me)
                throw new IllegalThreadStateException();
            group.add(this);
            start0();
            if (stopBeforeStart) {
        stop0(throwableFromStop);
    }
        }
    ===============
    这是原代码。
      

  3.   

    有了a.start()方法以后  就用自动运行run()方法。所以可以省略此处的a.run()方法
      

  4.   


    public class Test { public static void main(String[] args) {
    Test t = new Test();
    A a = t.new A();
    a.start();
    a.run();
    a.run2();  //试着以此注释掉这三个方法
    }

    class A extends Thread{
    @Override
    public void run() {
    System.out.println(currentThread().getName());
    }
    public void run2(){
    System.out.println(currentThread().getName());
    }
    }
    }
      

  5.   

    a.start()的输出结果不是第一个,结果如下:
    main
    main
    Thread-0
      

  6.   

    你的内部类是非静态的,你这么声明会报错把?
            Test t = new Test();
            A a = t.new A();  a.start();
      a.run();//为什么是主线程调用,是a调用自己的run方法啊
    为什么是主线程调用?那是因为你这么写就是主线程调用了内部类中的一个普通的方法,所以就是主线程调用