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

当前回答

T.start()是标准库为您的代码提供的方法,当您需要一个新线程时可以调用它。

R.run()是为库在新线程中调用提供的方法。


这些答案大多数都忽略了一个大问题,即就Java语言而言,t.t start()和r.r run()之间的差异并不比其他任何两个方法之间的差异大。

它们都是方法。它们都运行在调用它们的线程中。它们都做它们被编码要做的事情,然后它们都返回给它们的调用者,仍然在同一个线程中。

最大的区别是t.t start()的大部分代码是本地代码,而在大多数情况下,r.r run()的代码将是纯Java。但这并没有太大的区别。代码就是代码。原生代码更难找到,找到后也更难理解,但它仍然只是告诉计算机该做什么的代码。

那么,t.start()做什么呢?

它创建了一个新的本机线程,它安排该线程调用t.run(),然后它告诉操作系统让新线程运行。然后它又回来了。

r.run()做什么?

有趣的是,问这个问题的人正是写这个问题的人。r.r run()执行您(即编写它的开发人员)设计它要执行的任何操作。

其他回答

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

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

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

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

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

T.start()是标准库为您的代码提供的方法,当您需要一个新线程时可以调用它。

R.run()是为库在新线程中调用提供的方法。


这些答案大多数都忽略了一个大问题,即就Java语言而言,t.t start()和r.r run()之间的差异并不比其他任何两个方法之间的差异大。

它们都是方法。它们都运行在调用它们的线程中。它们都做它们被编码要做的事情,然后它们都返回给它们的调用者,仍然在同一个线程中。

最大的区别是t.t start()的大部分代码是本地代码,而在大多数情况下,r.r run()的代码将是纯Java。但这并没有太大的区别。代码就是代码。原生代码更难找到,找到后也更难理解,但它仍然只是告诉计算机该做什么的代码。

那么,t.start()做什么呢?

它创建了一个新的本机线程,它安排该线程调用t.run(),然后它告诉操作系统让新线程运行。然后它又回来了。

r.run()做什么?

有趣的是,问这个问题的人正是写这个问题的人。r.r run()执行您(即编写它的开发人员)设计它要执行的任何操作。

成员们提出的观点都是对的所以我想补充一点。问题是JAVA不支持多继承。但是如果你想从另一个类a中派生一个类B,但你只能从一个类中派生。现在的问题是如何从这两个类中“派生”:A和Thread。因此,您可以使用可运行接口。

public class ThreadTest{
   public void method(){
      Thread myThread = new Thread(new B());
      myThread.start;
   }
}

public class B extends A implements Runnable{...

如果直接调用run()方法,则没有使用多线程特性,因为run()方法是作为调用者线程的一部分执行的。

如果你在线程上调用start()方法,Java虚拟机将调用run()方法,两个线程将并发运行——当前线程(在你的例子中是main())和其他线程(在你的例子中是Runnable r1)。

看看线程类中的start()方法的源代码

 /**
     * Causes this thread to begin execution; the Java Virtual Machine
     * calls the <code>run</code> method of this thread.
     * <p>
     * The result is that two threads are running concurrently: the
     * current thread (which returns from the call to the
     * <code>start</code> method) and the other thread (which executes its
     * <code>run</code> method).
     * <p>
     * It is never legal to start a thread more than once.
     * In particular, a thread may not be restarted once it has completed
     * execution.
     *
     * @exception  IllegalThreadStateException  if the thread was already
     *               started.
     * @see        #run()
     * @see        #stop()
     */
    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();
        group.add(this);
        start0();
        if (stopBeforeStart) {
            stop0(throwableFromStop);
        }
    }

    private native void start0();

在上面的代码中,你看不到对run()方法的调用。

私有本机void start0()负责调用run()方法。JVM执行这个本机方法。