假设我们有两个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();
}

当前回答

第一个例子:没有多线程。两者都在单个(现有)线程中执行。没有线程创建。

R1 r1 = new R1();
R2 r2 = new R2();

r1和r2只是实现Runnable接口并因此实现run()方法的类的两个不同对象。当你调用r1.run()时,你是在当前线程中执行它。

第二个例子:两个独立的线程。

Thread t1 = new Thread(r1);
Thread t2 = new Thread(r2);

t1和t2是Thread类的对象。当调用t1.start()时,它会启动一个新线程,并在内部调用r1的run()方法以在该新线程中执行它。

其他回答

调用run()在调用线程上执行,就像任何其他方法调用一样。而thread. start()创建一个新线程。 调用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(),那么你根本就没有使用多线程的特性。

Thread.start()代码向调度器注册线程,调度器调用run()方法。同样,Thread是类,而Runnable是接口。

在第一种情况下,您只是调用r1和r2对象的run()方法。

在第二种情况下,您实际上创建了2个新线程!

Start()会在某个时刻调用run() !