要如何使用
setPriority(MAX_PRIORITY )
setPriority(MIN_PRIORITY )
setPriority(NORM_PRIORITY )
或
setPriority(int newPriority)
是要放在 run()裡.還是?
可以用以下這個例子來解答嗎~
class P9_697{
public static void main(String arg[]){
A a=new A();
B b=new B();
C c=new C();
a.start();
b.start();
c.start();
}
}
class A implements Runnable{
public void run(){
for(int i=0; i<=20; i++){
System.out.println("A :"+i);
}
}
}
class B implements Runnable{
public void run(){
for(int i=100; i>=80; i--){
System.out.println("B :"+i);
}
}
}
class C implements Runnable{
public void run(){
for(int i=200; i<=250; i++){
System.out.println("C :"+i);
}
}
}
2006-12-13 13:16:30 · 2 個解答 · 發問者 Vicky 2 in 電腦與網際網路 ➔ 程式設計
那這三種如何使用呢? compiler好像找不到這個方法耶?
setPriority(MAX_PRIORITY )
setPriority(MIN_PRIORITY )
setPriority(NORM_PRIORITY )
2006-12-14 12:50:26 · update #1
這三種預設的 priority 參數是屬於 Thread 的類別屬性,因此前面要加 Thread 來辨明之。請參考我的做法。
class P9_697{
public static void main(String arg[]){
Thread a = new Thread(new A());
Thread b = new Thread(new B());
Thread c = new Thread(new C());
a.setPriority(Thread.MIN_PRIORITY);
b.setPriority(Thread.NORM_PRIORITY);
c.setPriority(Thread.MAX_PRIORITY);
a.start();
b.start();
c.start();
}
}
class A implements Runnable{
public void run(){
try {
for(int i=0; i<=20; i++){
System.out.println("A :"+i);
Thread.sleep(1);
}
} catch (InterruptedException ie) {}
}
}
class B implements Runnable{
public void run(){
try {
for(int i=100; i>=80; i--){
System.out.println("B :"+i);
Thread.sleep(1);
}
} catch (InterruptedException ie) {}
}
}
class C implements Runnable{
public void run(){
try {
for(int i=200; i<=250; i++){
System.out.println("C :"+i);
Thread.sleep(1);
}
} catch (InterruptedException ie) {}
}
}
2006-12-18 12:27:23 · answer #1 · answered by ? 7 · 0⤊ 0⤋
class P9_697{
public static void main(String arg[]){
Thread a = new Thread(new A());
Thread b = new Thread(new B());
Thread c = new Thread(new C());
a.setPriority(10);
b.setPriority(5);
c.setPriority(1);
c.start();
b.start();
a.start();
}
}
2006-12-14 16:43:06 補充:
優先權從1~10
1最低
10最高
2006-12-14 11:42:35 · answer #2 · answered by ? 2 · 0⤊ 0⤋