2013-10-21 141 views
-1

請參閱下面的代碼。這是從Oracle教程頁面的修改後的代碼示例:Java線程優先

public class BadThreads { 
    static String message; 

private static class CorrectorThread extends Thread { 
    public void run() { 
     try { 
      sleep(1000); 
     } catch (InterruptedException e) {} 
     message = "Mares do eat oats."; 
     System.out.println("1: "+ message); 
    }  
} 

public static void main(String args[]) throws InterruptedException { 

    CorrectorThread c=new CorrectorThread(); 

    c.start(); 
    System.out.println("0: "+ message); 
    c.run(); 
    System.out.println("2: "+ message); 

    message = "Mares do not eat oats."; 
    System.out.println("3: "+ message); 

    Thread.sleep(2000); 
    System.out.println("4: "+ message); 
} 
} 

打印

0: null 
1: Mares do eat oats. 
1: Mares do eat oats. 
2: Mares do eat oats. 
3: Mares do not eat oats. 
4: Mares do not eat oats. 

和是好的。

當我在main方法註釋掉

c.run(); 

,我得到

0: null 
2: null 
3: Mares do not eat oats. 
1: Mares do eat oats. 
4: Mares do eat oats. 

主要是前Ç執行怎麼來的?線程c具有與其「父」線程相同的優先級主要

難道主要是可見的ç從而ç等待主要返回?這是沒有道理的,但是我能想到的唯一的事情。

// ============================

編輯: 更換

c.run(); 

c.join(); 

爲相同的效果和更精細的編程。

+0

http://arashmd.blogspot.com/2013/06/java-threading.html#synctr – 2013-10-21 12:27:24

+1

優先級只是一個提示,在許多情況下完全忽略,如果你不是'root'或'administrator'它只是很重要如果你的CPU使用率很高,我不建議你這樣做。 –

+0

Peter Lawrey:我知道公平是保證避免飢餓的一件事 - 雖然不能保證等待時間最長的線程第一回合,但沒有聽到高CPU利用率(?) – Roam

回答

1

當您撥打c.run()時,它會執行運行功能並等待完成。等待時間也會涵蓋線程時間並且在第一種情況下您會看到兩個連續輸出。在第二種情況下,線程啓動並且主要運行與run函數平行,因爲運行處於睡眠狀態,並且它覆蓋了主要的顯示語句。

c.start()啓動線程平行於主線程運行。調用運行函數,並在完成時移動到下一個語句。

+0

是的,是的。在這一天的這個時刻錯過了時差。謝謝。 – Roam

+0

@格雷 - 他的回答在你編輯之前看起來不錯。如果那當然好了。 – Roam

+0

@roam仍然感謝灰色,改善.... :) –

0

見聯註釋

CorrectorThread c=new CorrectorThread(); 

c.start(); // new thread started, but up to scheduler to decide when to give it a slice of time 
System.out.println("0: "+ message); 
c.run(); // ran in main thread 
System.out.println("2: "+ message); // as expected, message was set by the other thread 

message = "Mares do not eat oats."; 
System.out.println("3: "+ message); // as expected 

Thread.sleep(2000); 
System.out.println("4: "+ message); // as expected 

c.start()c.run()將是雙方最終調用CorrectorThread#run(),但在不同的線程。這取決於Thread調度程序,它將首先到達那裏。

在你不叫c.run()調度的例子有時間打電話給

System.out.println("0: "+ message); // null 

System.out.println("2: "+ message); // null 

其他線程有時間來初始化message之前。

+0

仔細閱讀Q. – Roam

+0

@Roam這是一場比賽,一個Thread設置'message',另一個設置它。 –

+0

你的答案改變了。我已經評論過 - 最初的一個。 – Roam

4

線程的重點在於它們並行執行。所以主線程與校正器線程並行執行。由於校正器線程所做的第一件事是休眠1秒,因此主線程有足夠的時間在校正器線程更改消息之前執行其指令。

+0

是的,時差。與我爲zeeshan mughal寫的評論一樣。 – Roam

+0

好奇你爲什麼選擇比這個更晚的答案,還有更差的@Roam。 – Gray

0

嘗試這樣的: -

public class BadThreads { 
    static String message; 

    private static class CorrectorThread extends Thread { 
     public void run() { 
      try { 
       sleep(1000); 
      } catch (InterruptedException e) {} 
      message = "Mares do eat oats."; 
      System.out.println("3: "+ message); 
     }  
    } 

    public static void main(String args[]) throws InterruptedException { 

     CorrectorThread c=new CorrectorThread(); 
     c.start(); 

     System.out.println("0: "+ message); 
     System.out.println("1: "+ message); 

     message = "Mares do not eat oats."; 
     System.out.println("2: "+ message); 

     Thread.sleep(2000); 
     System.out.println("4: "+ message); 
    } 
} 
+0

無法啓動無法運行。 – Roam

+0

因此更新,同樣請嘗試 –

+0

請你解釋一下,讓其他人可以學習。只要放下未註釋的代碼就不會教任何東西。 – Gray

1

這是什麼奇怪的傢伙在這裏,因爲沒有關於主線程保證進入的調用線程/方法的順序第一或第二becasue。

第一個c.start()開始一個新的線程,這意味着主要繼續它的工作。所以在第一種情況下,你有這樣的事情

0000->thread main starts the bad thread | bad thread sleeps for 1 second 
     and prints the message value(null) 
0001->thread main runs the run() method | bad thread still is sleeping 
     with its thread 
0002-> both thread are sleeping .... 
0003->..... 
1000->either bad thread or main thread changes the message value to "Mares do eat oats." (not sure which goes first!) 
1001->thread main prints("1:" +message) | bad thread prints("1:" +message) 
1002->thread main prints("1:" +message) | bad thread has terminated X 
1003->thread main changes the message value to "Mares do not eat oats." 
1004->main threads prints the message again and sleeps for 2 seconds 
2004->main thread prints the message again. 

而在第二種情況下,沒有呼叫run()由主線程,所以主線程不睡1秒如不良的線程做,只是嘗試打印郵件並將其值更改爲「Mares不吃燕麥」。並再次打印,然後休眠2秒鐘,然後在1秒鐘後(當主線程處於睡眠狀態時)喚醒壞線程時,它改變消息的值並打印它,主線程在2秒後打印由壞線程改變的消息。

答案在這裏,因爲執行順序,不保證哪個線程先行或後行。 this tutorial可能會幫助你的兄弟。