java Thread.start() 和 Thread.run() 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2674174/
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-10-29 22:18:41  来源:igfitidea点击:

What is the difference between Thread.start() and Thread.run()?

javamultithreadingconcurrency

提问by sgokhales

Why do we call the start()method, which in turn calls the run()method?
Can't we directly make a call to run()?

为什么我们调用start()方法,方法又调用run()方法呢?
我们不能直接打电话给run()吗?

Please give an example where there is a difference.

请举例说明有区别的地方。

回答by Marko

No, you can't. Calling run will execute run() method in the same thread, without starting new thread.

不,你不能。调用 run 将在同一个线程中执行 run() 方法,而不会启动新线程。

回答by Chez

Why do we call the start()method, which in turn calls the run()method?

为什么我们调用start()方法,方法又调用run()方法呢?

No that's imprecise. start()in turn does not call the run method. instead it starts the thread which executes the run method. This is native.

不,那是不精确的。start()反过来不调用 run 方法。相反,它启动执行 run 方法的线程。这是原生的。

Can't we directly make a call to run()?

我们不能直接打电话给run()吗?

If you call run()directly you don't start the thread, you just execute the method on the same caller method.

如果run()直接调用,则不会启动线程,只需在同一个调用方方法上执行该方法。

Please give an example where there is a difference.

请举例说明有区别的地方。

There are millions on the web. Hence I don't duplicate.

网络上有数百万。因此我不复制。

回答by aditya

Actually thread.start()creates a new thread and have its own execution scenario.

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

but thread.run()not creating any new thread, instead it execute the run method in the current running thread.

thread.run()不创建任何新线程,而是在当前运行的线程中执行 run 方法。

So guys if you are using thread.run()then think that what is the use of multi-threading if you want only one thread execute all run method.

所以伙计们,如果你正在使用,thread.run()那么如果你只想一个线程执行所有运行方法,那么多线程有什么用。

回答by PaulJWilliams

Because start() doesnt just call run(). It starts a new thread and in that threadcalls run().

因为 start() 不只是调用 run()。它启动一个新线程并在该线程中调用 run()。

回答by Venkat

you can't run directly the run() method. Whenever start your thread by using thread.start(), then the run() method has been called and performed the further operation.

你不能直接运行 run() 方法。每当使用 thread.start() 启动线程时,就会调用 run() 方法并执行进一步的操作。

回答by user3667402

Main difference is that when program calls start()method a new Thread is created and code inside run() method is executed in new Thread.If you call run()method directly no new Thread is created and code inside run() will execute on current Thread.

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

Most of the time calling run() is bug or programming mistake because caller has intention of calling start() to create new thread and this error can be detect by many static code coverage tools like findbugs. If you want to perform time consuming task than always call start() method otherwise your main thread will stuck while performing time consuming task if you call run() method directly. Another difference between start vs run in Java thread is that you can not call start() method twice on thread object. once started, second call of start() will throw IllegalStateException in Java while you can call run() method twice.

大多数情况下,调用 run() 是 bug 或编程错误,因为调用者有意调用 start() 来创建新线程,并且此错误可以被许多静态代码覆盖工具(如 findbugs)检测到。如果要执行耗时的任务,请不要总是调用 start() 方法,否则如果直接调用 run() 方法,主线程将在执行耗时的任务时卡住。在 Java 线程中 start 与 run 之间的另一个区别是您不能在线程对象上两次调用 start() 方法。一旦启动,第二次调用 start() 将在 Java 中抛出 IllegalStateException 而您可以调用 run() 方法两次。

回答by Java Enthusiast

If you call run() directly, the code gets executed in the calling thread. By calling start(), a new thread is created other than the main thread and is executed in parallel.

如果您直接调用 run(),代码将在调用线程中执行。通过调用start(),会创建一个新线程而不是主线程并并行执行。

回答by Asif Mushtaq

Because start();is synchronized and run();is simple/regular method. Same as java knows starting execution from main();method. As thread knows starting execution from run();

因为start();是同步的并且run();是简单/常规的方法。就像java知道从main();方法开始执行一样。由于线程知道从run();

Here is the Source code from ThreadClass:

这是Thread类的源代码:

run();code:

run();代码:

@Override
public void run() { // overriding from Runnable
        if (target != null) {
            target.run();
        }
}

start();code:

start();代码:

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

        /* Notify the group that this thread is about to be started
         * so that it can be added to the group's list of threads
         * and the group's unstarted count can be decremented. */
        group.add(this);

        boolean started = false;
        try {
            start0();
            started = true;
        } finally {
            try {
                if (!started) {
                    group.threadStartFailed(this);
                }
            } catch (Throwable ignore) {
                /* do nothing. If start0 threw a Throwable then
                  it will be passed up the call stack */
            }
        }
    }

In short start();is the manager of threads, how to manage etc. and run();is tarting point of thread's working.

简而言之start();是线程的管理者,如何管理等等,run();是线程工作的起点。

回答by Mallikarjuna

this is the work done by start method

这是 start 方法完成的工作

synchronized public void start()
{ 
    //it calls start0() method internally and start0() method does below
    //create a real child thread and register with thread scheduler
    //create runtime stack for child thread
    //call run() on underlying Runtime object
}