62,614
社区成员
发帖
与我相关
我的任务
分享
class TestThreadPriority extends Thread {
int flag;
TestThreadPriority ttp;
public TestThreadPriority(int x) {
this.flag = x;
}
public TestThreadPriority(int x, TestThreadPriority ttp) {
this.flag = x;
this.ttp = ttp;
}
public static void main(String argv[]) {
TestThreadPriority s = new TestThreadPriority(2);
TestThreadPriority t = new TestThreadPriority(1, s);
t.setPriority(1);
s.setPriority(MAX_PRIORITY);
t.start();
System.out.println("one");
s.start();
System.out.println("two");
}
public void run() {
for (int i = 0; i < 3; i++) {
if (flag == 1) {
System.out.println("Thread t");
try {
ttp.interrupt();
Thread.sleep(3000);
} catch (InterruptedException e) {
System.out.println("t sleep");
}
} else {
System.out.println("Thread s");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
System.out.println("s sleep");
}
}
}
}
}
public static void main(String argv[]){
Test t = new Test(1);
Test s = new Test(2);
t.setPriority(1);
s.setPriority(MAX_PRIORITY);
t.start();
System.out.println("one");
s.start();
//public void interrupt() 中断线程。
t.interrupt();
System.out.println("two");
}