2014-10-28 9 views
0

例如,爲什麼下面的代碼不輸出7的優先級?爲什麼新線程中getPriority的優先級與調用線程中的優先級不同?

public class Test { 
    public static void main(String[] args) { 
     Thread thread = new Thread(new A()); 
     thread.setPriority(7); 
     System.out.println("in main: " + thread.getPriority()); 
     thread.start(); 
    } 
} 

class A extends Thread { 
    @Override 
    public void run() { 
     System.out.println("in thread: " + this.getPriority()); 
    } 
} 

輸出:

in main: 7 
in thread: 5 

回答

4
new Thread(new A()); 

你治療new A()Runnable並將它傳遞給一個單獨的Thread實例。

新的Thread實例完全不影響的Thread基數。您可以直接使用new A()