0
我想優先給線程b但它不工作仍然有時打印線程t之前b優先級不工作,我不確定任何幫助的代碼請!如何在java線程中優先
public static void main(String[] args) throws Exception {
Thread t = new Thread(new one("this is t thread"));
Thread b = new Thread(new one("this is b thread"));
b.setPriority(10);
t.setPriority(4);
t.start();
b.start();
}
class one implements Runnable {
String name;
public one(String n) {
name = n;
}
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(name);
}
}
}
存在很多問題與這個例子就足夠了,但是,從根本上,你叫't.start()'第一。爲什麼它只是等待,看看你以後是否啓動了更高優先級的線程? – Affe
找到了! http://stackoverflow.com/questions/16289637/java-testing-thread-priority-how-come-in-some-cases-low-priority-threads-are-f/16289839#16289839 – Affe
謝謝你的幫助 – user2509219