我不明白启动和运行线程之间的区别,我测试了两种方法并输出了相同的结果,首先我在同一个线程上使用了run()和start的组合,并且它们执行的功能如下:
public class TestRunAndStart implements Runnable {
public void run() {
System.out.println("running");
}
public static void main(String[] args) {
Thread t = new Thread(new TestRunAndStart());
t.run();
t.run();
t.start();
}
Run Code Online (Sandbox Code Playgroud)
}
输出是:
running
running
running
Run Code Online (Sandbox Code Playgroud)
然后我看到run()方法的javadoc说:
If this thread was constructed using a separate Runnable run object, then that Runnable object's run method is called; otherwise, this method does nothing and returns 所以我尝试使用run()方法而没有单独的runnable,如下所示:
public class TestRun extends Thread {
public void run(){
System.out.println("Running Normal Thread");
}
public static void main(String[]args){
TestRun …Run Code Online (Sandbox Code Playgroud)