写一个子类继承Thread类,是为了重写里面的run()方法,也就是线程启动后真正执行的内容。 如果你直接Thread thread = new Thread();创建对象,语法上没有错的,但是run()方法是系统默认的,也不是你自己想定义的线程中的方法,那这个线程有什么意义呢?
/** * If this thread was constructed using a separate * <code>Runnable</code> run object, then that * <code>Runnable</code> object's <code>run</code> method is called; * otherwise, this method does nothing and returns. * <p> * Subclasses of <code>Thread</code> should override this method. * * @see #start() * @see #stop() * @see #Thread(ThreadGroup, Runnable, String) */ public void run() { if (target != null) { target.run(); } }这是jdk中Thread类run()方法的源码,一般自己写线程都要重定义覆盖这个方法吧,它默认的方法很难满足需求
如果你直接Thread thread = new Thread();创建对象,语法上没有错的,但是run()方法是系统默认的,也不是你自己想定义的线程中的方法,那这个线程有什么意义呢?
/**
* If this thread was constructed using a separate
* <code>Runnable</code> run object, then that
* <code>Runnable</code> object's <code>run</code> method is called;
* otherwise, this method does nothing and returns.
* <p>
* Subclasses of <code>Thread</code> should override this method.
*
* @see #start()
* @see #stop()
* @see #Thread(ThreadGroup, Runnable, String)
*/
public void run() {
if (target != null) {
target.run();
}
}这是jdk中Thread类run()方法的源码,一般自己写线程都要重定义覆盖这个方法吧,它默认的方法很难满足需求