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){
}
}
}
}
{
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){
}
}
}
}
a.start();为什么要下面这一句?
a.run();//为什么是主线程调用,是a调用自己的run方法啊
if (threadStatus != 0 || this != me)
throw new IllegalThreadStateException();
group.add(this);
start0();
if (stopBeforeStart) {
stop0(throwableFromStop);
}
}
===============
这是原代码。
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());
}
}
}
main
main
Thread-0
Test t = new Test();
A a = t.new A(); a.start();
a.run();//为什么是主线程调用,是a调用自己的run方法啊为什么是主线程调用?那是因为你这么写就是主线程调用了内部类中的一个普通的方法,所以就是主线程调用