62,612
社区成员
发帖
与我相关
我的任务
分享
public class TestPriority {
public static void main(String[] args) throws Exception {
Thread t1 = new Thread(new PriorityT1());
Thread t2 = new Thread(new PriorityT2());
t1.setPriority(Thread.NORM_PRIORITY + 3);//增加优先级
t1.start();
t2.start();
Thread.sleep(100);
synchronized (TestPriority.class) {
TestPriority.class.notifyAll();
}
}
}
class PriorityT1 implements Runnable {
public void run() {
synchronized (TestPriority.class) {
try {
TestPriority.class.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for (int i = 0; i < 1000; i++) {
System.out.println("T1:" + i);
}
}
}
class PriorityT2 implements Runnable {
public void run() {
synchronized (TestPriority.class) {
try {
TestPriority.class.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for (int i = 0; i < 1000; i++) {
System.out.println("--T2:" + i);
}
}
}