假设我们有两个Runnables:

class R1 implements Runnable {
    public void run() { … }
    …
}

class R2 implements Runnable {
    public void run() { … }
    …
}

那么这两者的区别是什么呢:

public static void main() {
    R1 r1 = new R1();
    R2 r2 = new R2();

    r1.run();
    r2.run();
}

这:

public static void main() {
    R1 r1 = new R1();
    R2 r2 = new R2();
    Thread t1 = new Thread(r1);
    Thread t2 = new Thread(r2);

    t1.start();
    t2.start();
}

当前回答

如果你在main方法中执行run(), main方法的线程将调用run方法,而不是你需要运行的线程。

start()方法创建了新的线程,run()方法必须为此执行

其他回答

区别在于,当程序调用start()方法时,将创建一个新线程,run()方法中的代码将在新线程中执行:而如果直接调用run()方法,则不会创建新线程,run()方法中的代码将直接在当前线程中执行。

Java线程中start()和run()的另一个区别是不能调用start()两次。一旦启动,第二次start()调用将在Java中抛出IllegalStateException,而您可以多次调用run()方法,因为它只是一个普通的方法。

Thread类中单独的start()和run()方法提供了两种创建线程程序的方法。start()方法启动新线程的执行,并调用run()方法。start()方法立即返回,新线程通常会继续,直到run()方法返回。

Thread类的run()方法什么也不做,所以子类应该用在第二个线程中执行的代码重写该方法。如果一个线程是用Runnable参数实例化的,线程的run()方法会在新线程中执行Runnable对象的run()方法。

根据线程程序的性质,直接调用Thread run()方法可以得到与通过start()方法调用相同的输出,但在后一种情况下,代码实际上是在一个新线程中执行的。

区别在于thread. start()启动一个调用run()方法的线程,而Runnable.run()只调用当前线程上的run()方法。

Start()方法调用Thread扩展类和Runnable实现接口的运行重写方法。

但是通过调用run()它会搜索run方法,但如果类实现了Runnable接口,那么它会调用run()重写Runnable方法。

例:

`

public class Main1
{
A a=new A();
B b=new B();
a.run();//This call run() of Thread because run() of Thread only call when class 
        //implements with Runnable not when class extends Thread.
b.run();//This not run anything because no run method found in class B but it 
        //didn't show any error.

a.start();//this call run() of Thread
b.start();//this call run() of Thread
}

class A implements Runnable{
@Override
    public void run() {
            System.out.println("A ");
    }
}

class B extends Thread {

    @Override
    public void run() {
            System.out.println("B ");
    }
}

`

实际上,thread .start()创建了一个新线程,并有自己的执行场景。

Thread.start()异步调用run()方法,将新线程的状态更改为Runnable。

但是thread. run()不会创建任何新的线程。相反,它在当前运行的线程中同步执行run方法。

如果你正在使用Thread.run(),那么你根本就没有使用多线程的特性。