线程运行快慢测试
public class ThreadA extends Thread {
private int count = 0;
public int getCount() {
return count;
}
@Override
public void run() {
while (true) {
count++;
}
}
}
public class ThreadB extends Thread {
private int count = 0;
public int getCount() {
return count;
}
@Override
public void run() {
while (true) {
count++;
}
}
}
public class Run {
public static void main(String[] args) {
try {
ThreadA a = new ThreadA();
//public final static int NORM_PRIORITY = 5;
a.setPriority(Thread.NORM_PRIORITY - 3);
a.start();
ThreadB b = new ThreadB();
//public final static int NORM_PRIORITY = 5;
b.setPriority(Thread.NORM_PRIORITY + 3);
b.start();
Thread.sleep(20000);
a.stop();
b.stop();
System.out.println("a=" + a.getCount());
System.out.println("b=" + b.getCount());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
结果
优先级高的运行的快
a=663977053
b=734505548