优先级问题

来源:互联网 发布:淘宝买汽车配件 编辑:程序博客网 时间:2024/06/06 01:30

优先级实例


//数字越大,优先级越高(1-10)
public class Priority {


public final static int MIN_PRIORITY = 1;
public final static int NORM_PRIORITY = 5;
public final static int MAX_PRIORITY = 10;


public static class HightPriority extends Thread {
static int count = 0;


@Override
public void run() {
while (true) {
synchronized (Priority.class) {
count++;
if (count > 100000) {
System.out.println("HightPriority is complete!");
break;
}
}


}
}
}


public static class LowPriority extends Thread {
static int count = 0;


@Override
public void run() {
while (true) {
synchronized (Priority.class) {
count++;
if (count > 100000) {
System.out.println("LowPriority is complete!");
break;
}
}


}
}
}


public static void main(String[] args) throws InterruptedException {
Thread high = new HightPriority();
Thread low = new LowPriority();


high.setPriority(Thread.MAX_PRIORITY);
low.setPriority(Thread.MIN_PRIORITY);


low.start();
high.start();


}


}


大部分情况下,高优先级都会首先完成,不过有时候也会出现低优先级的先完成。


每天努力一点,每天都在进步!

原创粉丝点击