首先是多线程的概念:
多线程
多线程是异步的,和单任务不一样,并不必定按照代码的执行顺序(上图左)来运行,而是交错占用CPU运行(上图右);app
JAVA多线程有两种实现方式:一、继承Thread类; 二、实现Runnable接口异步
其中实现Runnable接口是Java多线程的主要实现方法,由于JAVA的单继承特性,一旦继承了Thread类,就不能再继承别的类。而JAVA类能够继承多个接口。
ide
public class Thread extends Thread { @Override public void run(){ //super.run(); 调用父类的run方法 System.out.println("success!"); } } public class Run { public static void main(String[] args) { Thread a = new Thread(); a.start(); System.out.println("end!"); } }
其中 @Override 是JAVA注解,代码重写一个父类的函数,继承Thread类实现多线程,必须重写 run() 方法。函数
问题一:为何要写 super.run();既然是重写父类的 run() 方法为何还要写 super.run() 呢?ui
首先是 Thread run()方法 源码:this
//Thread类重写了Runnable接口的run()方法。
//该run()方法首先判断当前是否有Runnable的实现target存在。 若是存在就执行target.run()
private Runnable target;
@Override
public void run() {
if (target != null) {
target.run();
}
}
原来Thread类也是实现了Runnable接口;在run()方法中,首先会检查target是否为空,若是不是,则执行该target的run()方法。spa
首先上结论:.net
一、无论传入的Target是否为空,首先都会执行Thread本身的run()方法。若是重写了该方法且该方法中没有super.run(),那么是永远不会调用Runnable实现的run()方法;线程
二、若是没有重写该方法,则会去判断target是否为空,以此来决定调用target实现的run()方法;
三、若是重写了该方法,且该方法中有super.run(),在执行完该语句以前的全部代码后,会判断target是否为空,以此来决定调用target实现的run()方法
具体实验: https://blog.csdn.net/guguituzi/article/details/44593863
因此,super.run();正常状况下能够不写,可是我看到是书上都习惯性带上 super.run() ,知道为何的小伙伴很是感谢可以给我留言告诉我答案。
问题二: .start() 源码
/* JVM调用此线程的run方法。*/ public synchronized void start() { /** * This method is not invoked for the main method thread or "system" * group threads created/set up by the VM. Any new functionality added * to this method in the future may have to also be added to the VM. * * A zero status value corresponds to state "NEW". */ //此判断当前线程只能被启动一次,不能被重复启动 if (threadStatus != 0) throw new IllegalThreadStateException(); /* Notify the group that this thread is about to be started * so that it can be added to the group's list of threads * and the group's unstarted count can be decremented. */ /*通知组该线程即将启动 *这样它就能够添加到组的线程列表中 *而且该组的未启动计数能够递减。*/ group.add(this); boolean started = false; try { start0(); started = true; } finally { try { // 若是线程启动失败,从线程组里面移除该线程 if (!started) { group.threadStartFailed(this); } } catch (Throwable ignore) { /* do nothing. If start0 threw a Throwable then it will be passed up the call stack */ } } }
另外,线程名.start() 的顺序,不表明线程的执行顺序!
public class Thread_2 implements Runnable{ @Override public void run(){ System.out.println("i'm running!..."); } }
实现原理和继承Thread相似,再也不赘述。