在网上看到一个被多次转载的观点:
Solaris:相同优先级的线程不能相互抢占对方的CPU时间
经过Google发现出自Java原著,原文是:
For the runtime on a Solaris Operating Environment platform, Java technology does not preempt threads of the same priority. 我下载了一个Solaris 10的X86版本安装在了VMWare,进行了测试,测试代码如下class Test {
public static void main(String[] args) {
Thread t1 = new Thread(new T1());
Thread t2 = new Thread(new T2());
t1.setPriority(Thread.NORM_PRIORITY);
t2.setPriority(Thread.NORM_PRIORITY);
t1.start();
t2.start();
}
}class T1 implements Runnable {
public void run() {
for(int i=0; i<10000; i++) {
System.out.println("T1: " + i);
}
}
}class T2 implements Runnable {
public void run() {
for(int i=0; i<10000; i++) {
System.out.println("------T2: " + i);
}
}
}
以小弟愚见,如果Solaris系统下相同优先级的线程不能相互抢占对方的CPU时间,那么本测试代码中,t1和t2的优先级都是NORM_PRIORITY,就应该执行完一个再执行另一个。结果不然,在T2循环到i=1625时,T1就插进来执行了,随后又有几次交替
这应该可以得出结论:相同优先级的几个进程是可以相互抢占对方的CPU时间
这是怎么回事呢?是我的测试有问题,还是java原著写错了?还是Solaris 10修改了代码改变了多线程的方式??