java中 start 为什么可以自动调用run ?

class A extends Thread
{
public void run()
{
g();
while (true)
{
System.out.println("AAAA");
}
}

public void g()
{
}
}

public class TestThread_1
{
public static void main(String[] args)
{
A aa = new A();
aa.start(); //aa.start(); 会自动调用run方法
//aa.start();
while (true)
{
System.out.println("BBBB");
}
}
}

线程 通过继承Thread或者实现Runnable接口实现
他们都要重写run方法
start方法是启动线程 启动线程后 线程会自动执行run方法
启动start前
这时此线程是处于就绪状态,
并没有运行。
然后通过此Thread类调用方法run()来完成其运行操作的,
这里方法run()称为线程体,
它包含了要执行的这个线程的内容,
Run方法运行结束,
此线程终止,
而CPU再运行其它线程,

而如果直接用Run方法,
这只是调用一个方法而已,
程序中依然只有主线程--这一个线程,
其程序执行路径还是只有一条,
这样就没有达到写线程的目的。

记住:线程就是为了更好地利用CPU,
提高程序运行速率的!
温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2017-10-01
原因很简单的,是因为类Thread中的start方法中,调用了Thread中的run方法。
顺便说下,类A继承了Tread类,在A中写run方法,就会覆盖掉Thread中的run方法,所以此时调用start方法后,实现的是自己的run方法体里面的代码。。。本回答被提问者采纳
第2个回答  2015-10-11
/**
 * Causes this thread to begin execution; the Java Virtual Machine
 * calls the <code>run</code> method of this thread.
 * <p>
 * The result is that two threads are running concurrently: the
 * current thread (which returns from the call to the
 * <code>start</code> method) and the other thread (which executes its
 * <code>run</code> method).
 * <p>
 * It is never legal to start a thread more than once.
 * In particular, a thread may not be restarted once it has completed
 * execution.
 *
 * @exception  IllegalThreadStateException  if the thread was already
 *               started.
 * @see        #run()
 * @see        #stop()
 */
public synchronized void start()

这个问题有点意思,因为在Thread的start中并没有显式调用run方法,不过研究上面的start这个方法的注释,注意第一行就可以发现jvm会调用thread的run()方法

第3个回答  2014-02-19
父类Thread的start()方法最终执行了run(),
子类只需实现(重写)run()方法即可
第4个回答  2014-02-19
看看start()方法的源码你就会知道了,它就不能在start中再调run么?我没看过源码你可以去看看应该是这样的追问

请问start()方法是在哪个类找呢?

追答

Thread中找,你反编译下