【问题标题】:What's the difference between Thread start() and Runnable run()Thread start() 和 Runnable run() 有什么区别
【发布时间】:2012-01-24 15:57:39
【问题描述】:

假设我们有这两个 Runnable:

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();
}

【问题讨论】:

    标签: java multithreading concurrency runnable


    【解决方案1】:

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

    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() 方法以在该新线程中执行它。

    【讨论】:

    • 我可以考虑在我们调用 Thread#start() 之前,没有真正与 os 线程相关的事情发生吗?它只是一个 java 对象。
    • 根据文档,这是正确的。检查线程对象初始化代码,符合文档。同样在源代码中,是start(),它正在调用一个natvie方法,这一定是在使os线程相关的事情发生。
    • 线程构造函数文档是here。线程对象初始化源为here。 start() 方法源是here。
    【解决方案2】:

    如果您只是直接调用run(),它会在调用线程上执行,就像任何其他方法调用一样。需要Thread.start() 才能实际创建一个新线程,以便并行执行runnable 的run 方法。

    【讨论】:

    • 在Hotspot JVM中,java线程和native线程有直接的映射关系。 Thread.start() 调用使线程状态从 new 状态移动到 Runnable 状态。 Runnable 并不意味着线程正在运行。原生线程初始化后,原生线程调用Java线程中的run()方法,使线程状态从Runnable变为Running。当线程终止时,本机线程和 Java 线程的所有资源都会被释放。
    • @overexchange 在哪里可以找到关于状态变化的资料。
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

      • @Ravindrababu 请去掉粗体字。它没有帮助。
      【解决方案5】:

      其实Thread.start()新建了一个线程,有自己的执行场景。

      Thread.start()异步调用run()方法,将new Thread的状态变为Runnable。

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

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

      【讨论】:

        【解决方案6】:

        invoke run() 正在调用线程上执行,就像任何其他方法调用一样。而Thread.start() 创建一个新线程。 调用 run() 是一个程序性错误。

        【讨论】:

          【解决方案7】:

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

          start() 方法创建新线程,run() 方法必须为其完成

          【讨论】:

          • 与'main方法'无关。
          • @EJP,main 作者的意思是调用方法。他的回答非常好。 +1 ;-)
          • @dom_beau 如果这是他的意思,他应该这么说。他说的不对。这个答案没有什么“很好”的。这只是一个混乱的混乱。
          【解决方案8】:

          t.start() 是库提供的方法,供您的代码在您需要新线程时调用。

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


          这些答案中的大多数都忽略了大局,即就 Java 语言而言,t.start() 和 r.run() 之间的区别并不比其他任何两种方法之间的区别更大。

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

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

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

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

          r.run() 是做什么的?

          有趣的是,提出这个问题的人正是写这个问题的人。 r.run() 做任何你(即编写它的开发人员)设计它做的事情。

          【讨论】:

            【解决方案9】:

            Thread.start() 代码向调度程序注册线程,调度程序调用run() 方法。另外,Thread 是类,Runnable 是接口。

            【讨论】:

              【解决方案10】:

              成员们提出的观点都很好,所以我只想补充一些东西。问题是 JAVA 不支持多重继承。但是,如果你想从另一个类 A 派生一个类 B,但你只能从一个类派生,那又如何呢?现在的问题是如何从两个类“派生”:A 和 Thread。因此,您可以使用 Runnable 接口。

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

              【讨论】:

              • 借助有关 Runnable - 一个接口和 Thread - 一个类的示例很好地解释了 run() 方法
              【解决方案11】:

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

              如果您在 Thread 上调用 start() 方法,Java 虚拟机将调用 run() 方法并且两个线程将同时运行 - 当前线程(在您的示例中为 main())和其他线程(在您的示例中可运行 r1示例)。

              查看Thread class中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() 方法的调用。

              private native void start0()负责调用run()方法。 JVM 执行这个本地方法。

              【讨论】:

                【解决方案12】:

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

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

                start() 会在某个时候调用run()!

                【讨论】:

                • 实际上,start() 不会调用 run():如果调用了,run() 方法将由调用 start() 的同一线程执行。 start() 将创建一个调用 run() 方法的线程。
                【解决方案13】:

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

                Thread 类的 run() 方法什么都不做,所以子类应该用代码覆盖该方法以在第二个线程中执行。如果使用 Runnable 参数实例化 Thread,则该线程的 run() 方法将在新线程中执行 Runnable 对象的 run() 方法。

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

                【讨论】:

                • 调用 `run()' 不是创建线程程序的方法。只有一种方法。
                【解决方案14】:

                Start()方法调用Thread扩展类的run override方法,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 ");
                    }
                }
                

                `

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 2017-03-29
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多