你什么时候叫java';s thread.run()而不是thread.start()?

你什么时候叫java';s thread.run()而不是thread.start()?,java,multithreading,concurrency,Java,Multithreading,Concurrency,什么时候调用Java的thread.run()而不是thread.start()?调用thread.start(),它将依次调用thread.run()。想不出你想绕过thread.start()并直接转到thread.run()从不。直接调用run()只需同步执行代码(在同一线程中),就像普通的方法调用一样。当您希望它同步运行时。调用run方法实际上不会提供多线程。start方法创建一个调用run方法的新线程。采用以下形式: 问:a和B的区别是什么 线程的start()和run()方法 答:T

什么时候调用Java的
thread.run()
而不是
thread.start()

调用
thread.start()
,它将依次调用
thread.run()
。想不出你想绕过
thread.start()
并直接转到
thread.run()
从不。直接调用run()只需同步执行代码(在同一线程中),就像普通的方法调用一样。

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

采用以下形式:

问:a和B的区别是什么 线程的start()和run()方法

答:Thread类中单独的start()和run()方法提供 创建线程程序的两种方法。 start()方法启动 新线程和调用的执行 run()方法。start()方法 立即返回新线程 通常持续到运行()为止 方法返回

线程类的run()方法不执行任何操作,因此子类应该 用代码重写该方法以 在第二个线程中执行。如果 线程被实例化为可运行线程 参数,线程的run()方法 执行的run()方法 新线程中的可运行对象 相反

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


如果希望像执行任何其他方法一样执行run()的内容。当然,不要启动线程。

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

假设您知道启动和运行方法的用法,即同步与异步;run方法仅可用于测试功能


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

至少在JVM 1.6中,有一点检查,run被本机调用:

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

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

更好的是,为了实现更多的解耦,请查看JDK 5和更新版本中的
Executor
接口和框架。简而言之,这允许您将任务执行(Runnable实例)与执行方式(Executor实现,它可以在当前线程中、在新线程中、使用池中的现有线程等执行Runnable)分离。

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

线程类'
run()
方法不执行任何操作,因此子类应使用代码重写该方法,以便在第二个线程中执行。如果线程是用Runnable参数实例化的,那么线程的
run()
方法将在新线程中执行Runnable对象的
run()
方法

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

如果问题是“为什么调用线程启动方法而不是直接运行方法”,那么我已经用下面的示例代码进行了回答。希望这能澄清问题。 在以下示例中:

/*
By calling t1.start(), 
we are getting the main calling thread returned immediately 
after the t1.start() called and is ready to proceed for other 
operations.And the thread t1 starts executing the run method of the object r. 
Hence the the output will be:

      I am the main thread , i created thread t1 and had it execute run method, which is currently looping from 0 to 1000000

      I am done executing run method of testThread

*/


/* If we call t1.run() instead of t1.start(), (just replace t1.start() with t1.run() in the code for testing)
 its like a regular method call and the main thread will not return until the run method completes, 
 hence the output will be:

         I am done executing run method of testThread

         I am the main thread , i created thread t1 and had it execute run method, which is currently looping for i to 1000000

*/


class testThread implements Runnable{

 public void run()
 {
     for(int i=0;i<1000000;i++){} //a simple delay block to clarify.

     System.out.println("I am done executing run method of testThread");

 }  
}

public class mainClass{

   public static void main(String [] args)
    {
          testThread r = new testThread();
          Thread t1 = new Thread(r);
          t1.start();  /* Question is: can we call instead t1.run() */  
          System.out.println("I am the main thread , i created thread t1 and had it execute run method, which is currently looping for i to 1000000");

    }
}
/*
通过调用t1.start(),
我们将立即返回主调用线程
在t1.start()调用并准备好继续其他
线程t1开始执行对象r的run方法。
因此,输出将为:
我是主线程,我创建了线程t1,并让它执行run方法,该方法当前从0循环到1000000
我已经完成了testThread的run方法的执行
*/
/*如果我们调用t1.run()而不是t1.start(),(只需在测试代码中将t1.start()替换为t1.run()
它类似于常规方法调用,在run方法完成之前,主线程不会返回,
因此,输出将为:
我已经完成了testThread的run方法的执行
我是主线程,我创建了线程t1,并让它执行run方法,该方法目前正在循环I到1000000
*/
类testThread实现可运行{
公开募捐
{
for(int i=0;i执行
thread.run()
不会创建一个新的
thread
,在其中执行代码。它只是在调用
thread.run()
代码的当前线程中执行代码

执行
thread.start()
将创建一个新的操作系统级线程,在该线程中调用
run()
方法

实质上:

单线程编程→ 直接调用
run()
方法

多线程编程→ 调用
start()
方法


此外,正如其他人提到的,“测试”似乎是唯一可取的情况,在这种情况下,您可以直接从代码调用
run()

请注意上面的重要评论
public class TestClass implements Runnable {
    public static void main(String[] args) {
        TestClass tc = new TestClass();

        Thread t1 = new Thread(tc);
        System.out.println("Before Starting Thread " + Thread.currentThread().hashCode());
        t1.start();
        System.out.println("After Starting Thread " + Thread.currentThread().hashCode());
    }

    @Override
    public void run() {
        System.out.println("TestClass Run method is  Running with thread " + Thread.currentThread().hashCode());        
    }
}