你什么时候会调用java的thread.run()而不是thread.start()?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/262816/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 12:11:17  来源:igfitidea点击:

When would you call java's thread.run() instead of thread.start()?

javamultithreadingconcurrency

提问by blank

When would you call Java's thread.run()instead of thread.start()?

你什么时候会调用 Javathread.run()而不是thread.start()

采纳答案by Paul Croarkin

You might want to call run() in a particular unit test that is concerned strictly with functionality and not with concurrency.

您可能希望在严格关注功能而非并发性的特定单元测试中调用 run()。

回答by Chris Ballance

Call thread.start(), it will in turn call thread.run(). Can't think of a case when you would want to bypass thread.start()and go directly to thread.run()

呼叫thread.start(),它会依次呼叫thread.run()。想不出你想绕过thread.start()直接去的情况thread.run()

回答by Adam Crume

Never. Calling run() directly just executes the code synchronously (in the same thread), just like a normal method call.

绝不。直接调用 run() 只是同步执行代码(在同一个线程中),就像普通的方法调用一样。

回答by Brian

When you want it to run synchronously. Calling the run method won't actually give you multi-threading. The start method creates a new thread which calls the run method.

当您希望它同步运行时。调用 run 方法实际上不会给你多线程。start 方法创建一个调用 run 方法的新线程。

回答by Tomalak

Taken form the Code Style Java threads FAQ:

采用Code Style Java 线程常见问题解答

Q: What's the difference between a thread's start() and run() methods?

A: The separate start() and run() methods in the Thread class provide two ways to create threaded programs. The start() method starts the execution of the new thread and calls the run() method. The start() method returns immediately and the new thread normally continues until the run() method returns.

The Thread class' run() method does nothing, so sub-classes should override the method with code to execute in the second thread. If a Thread is instantiated with a Runnable argument, the thread's run() method executes the run() method of the Runnable object in the new thread instead.

Depending on the nature of your threaded program, calling the Thread run() method directly can give the same output as calling via the start() method, but in the latter case the code is actually executed in a new thread.

问:线程的 start() 和 run() 方法有什么区别?

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

Thread 类的 run() 方法不执行任何操作,因此子类应使用要在第二个线程中执行的代码覆盖该方法。如果使用 Runnable 参数实例化 Thread,则该线程的 run() 方法将在新线程中执行 Runnable 对象的 run() 方法。

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

回答by Tomalak

If you want to execute the contents of run() like you would of any other method. Not to start a thread, of course.

如果您想像执行任何其他方法一样执行 run() 的内容。当然,不是开始一个线程。

回答by Salman Kasbati

Assuming that you know the start and run method usage i.e. synchronous vs. asynchronous; run method can be used just to test the functionality.

假设您知道 start 和 run 方法的用法,即同步与异步;run 方法可以仅用于测试功能。

Plus in some circumstances, the same thread class can be used in two different places with synch and asynch functionality requirements by having two different objects with one's run method and other's start method being invoked.

此外,在某些情况下,通过调用一个的 run 方法和另一个的 start 方法的两个不同对象,可以在具有同步和异步功能要求的两个不同地方使用相同的线程类。

回答by Steve B.

At least in the JVM 1.6., there's a bit of checking and run is called natively:

至少在 JVM 1.6 中,有一些检查和运行被本地调用:

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

回答by Scott Bale

This has already been alluded to, but just to be clear: creating a new Thread object only to call it's run() method is needlessly expensive and should be a major red flag. It would be a much better, more decoupled design to create a Runnable impl and either (a) call it'srun() method directly if that's the desired behavior, or (b) construct a new Thread with that Runnable and start the Thread.

这已经被提及,但要明确一点:创建一个新的 Thread 对象只是为了调用它的 run() 方法是不必要的昂贵,应该是一个主要的危险信号。创建一个 Runnable impl 并(a)直接调用它的run() 方法(如果这是所需的行为),或者(b)使用该 Runnable 构造一个新线程并启动该线程将是一个更好、更解耦的设计。

Better yet, for even more decoupling, check out the Executorinterface and framework in JDK 5 and newer. This allows you, in a nutshell, to decouple task execution (the Runnable instance) from howit is executed (the Executor implementation, which might execute the Runnable in the current Thread, in a new Thread, using an existing Thread from a pool, and whatnot).

更好的是,为了更多的解耦,请查看ExecutorJDK 5 和更新版本中的接口和框架。这使您,简单地说,解耦任务执行(Runnable的实例)从如何执行它(的Executor实现,这可能会在当前线程从池中执行的Runnable,在一个新的线程,使用现有的线程,什么的)。

回答by alok

The separate start()and run()methods in the Thread class provide two ways to create threaded programs. The start()method starts the execution of the new thread and calls the run()method. The start()method returns immediately and the new thread normally continues until the run()method returns.

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

The Thread class' run()method does nothing, so sub-classes should override the method with code to execute in the second thread. If a Thread is instantiated with a Runnable argument, the thread's run()method executes the run()method of the Runnable object in the new thread instead.

Thread 类的run()方法不执行任何操作,因此子类应使用要在第二个线程中执行的代码覆盖该方法。如果使用 Runnable 参数实例化 Thread,则该线程的run()方法将run()改为在新线程中执行Runnable 对象的方法。

Depending on the nature of your threaded program, calling the Thread run()method directly can give the same output as calling via the start()method, but in the latter case the code is actually executed in a new thread.

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

reference

参考