簡體   English   中英

你何時會調用java的thread.run()而不是thread.start()?

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

你何時會調用Java的thread.run()而不是thread.start()

您可能希望在特定單元測試中調用run(),該測試嚴格關注功能而不是並發。

決不。 直接調用run()只是同步執行代碼(在同一個線程中),就像普通的方法調用一樣。

采用代碼樣式Java線程FAQ

問:線程的start()和run()方法有什么區別?

答:Thread類中的單獨start()和run()方法提供了兩種創建線程程序的方法。 start()方法開始執行新線程並調用run()方法。 start()方法立即返回,新線程通常會繼續,直到run()方法返回。

Thread類的run()方法不執行任何操作,因此子類應該使用代碼覆蓋該方法以在第二個線程中執行。 如果使用Runnable參數實例化Thread,則線程的run()方法將在新線程中執行Runnable對象的run()方法。

根據線程程序的性質,直接調用Thread run()方法可以提供與通過start()方法調用相同的輸出,但在后一種情況下,代碼實際上是在新線程中執行的。

執行thread.run()不會創建一個執行代碼的新Thread 它只執行調用thread.run()代碼的當前Thread中的代碼。

執行thread.start()會創建一個新的OS級別線程,其中run()方法被調用。

在本質上:

單線程編程→直接調用run()方法

多線程編程→調用start()方法

此外,正如其他人所提到的,'testing'似乎是唯一可行的情況,您可以直接從代碼中調用run()

這已經被提到了,但只是要明確:創建一個新的Thread對象只是為了調用它的run()方法是不必要的昂貴,應該是一個主要的紅旗。 創建一個Runnable impl並且(a)直接調用它的 run()方法(如果這是所需的行為),或(b)用該Runnable構造一個新的Thread並啟動Thread,這將是一個更好,更分離的設計。

更好的是,為了進一步解耦,請查看JDK 5及更高版本中的Executor接口和框架。 這使您,簡單地說,從它是如何執行(執行人執行,這可能會從池中執行的Runnable在當前線程,在一個新的線程,使用現有的線程脫鈎任務執行(Runnable的實例),和諸如此類的東西)。

調用thread.start() ,它將依次調用thread.run() 當你想要繞過thread.start()並直接轉到thread.run()時,想不到一個案例

Thread類中的單獨start()run()方法提供了兩種創建線程程序的方法。 start()方法開始執行新線程並調用run()方法。 start()方法立即返回,新線程通常會繼續,直到run()方法返回。

Thread類的run()方法不執行任何操作,因此子類應該使用代碼覆蓋該方法以在第二個線程中執行。 如果使用Runnable參數實例化Thread,則線程的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");

    }
}

當您希望它同步運行時。 調用run方法實際上不會給你多線程。 start方法創建一個調用run方法的新線程。

如果你想像任何其他方法那樣執行run()的內容。 當然不要開始一個帖子。

假設你知道啟動和運行方法的用法,即同步與異步; run方法只能用於測試功能。

此外,在某些情況下,同一個線程類可以在具有同步和異步功能要求的兩個不同位置使用,方法是使用一個run方法和另一個start方法調用兩個不同的對象。

只需注意以上很棒的評論:有時你會編寫一個多線程代碼,它使用“start”方法來運行不同的線程。 如果使用“run”(而不是“start”)進行調試,您會發現它更容易,因為它使代碼同步運行並更容易調試。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM