JAVA 多线程编程 --线程的调度策略

JAVA中一个线程有自己的优先级(priority)
getPriority:返回当前线程的优先级
setPriority:设置当前线程的优先级
 
既然JAVA支持多线程,那么多个线程同时执行的时候必然涉及到调度的问题。
通过研究官方文档和书籍,我发现:JAVA的调度算法非常简单:
 
1.选择当前可运行线程中优先级最高的线程运行
2.拥有同样优先级的线程:采用round-robin的方式。
 
JAVA是preemptive的,但是不同平台的实现并不一定能保证这一点。
下面是一段引用:
For the runtime on a Solaris Operating Environment platform,Java technology does not preempt threads of the same priority.However,the runtime on Microsoft Windows platforms uses time-slicing,so it preempts threads of the same priority and even threads of higher priority.Preemption is not guaranteed;however,most JVM implementations result in behavior that appears to be strictly preemptive.Across JVM implementations,there is no absolute guarantee of preemption or time-slicing.The only guarantees lie in the coder’s use of wait and sleep. The model of a preemptive scheduler is that many threads might be runnable,but only one thread is actually running.This thread continues to run until it ceases to be runnable or another thread of higher priority becomes runnable.In the latter case,the lower priority thread is preempted by the thread of higher priority,which gets a chance to run instead.
 
看了这段引用后,发现讨论JAVA线程的调度似乎没有什么意义了,呵呵。
下面我是我的一个运行结果。我是win7,双核处理器。从结果看出来,基本还是满足前面两点的。
package multithread;


public class PrioritedThread extends Thread{
   
    public PrioritedThread(int i){
        if(i >0 && i < 11){
            this.setPriority(i);
        }
    }
   
    public void run(){
        int i = 0;
        while(true){
            if(i == 100000000){
                System.out.print(this.getPriority() + "\t");
                i = 0;
            }
            i++;
        }
    }


}


 
package multithread;


public class TestPriority {


   
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        for(int i = 1; i < 11; i++){
            Thread temp = new PrioritedThread(i);
            temp.start();
        }


    }


}


运行结果:
10    9    10    10    9    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    9    10    10    9    10    10    9    10    10    9    10    9    10    10    9    10    10    9    10    10    9    10    9    10    10    9    10    10    9    10    10    9    10    9    10    10    9    10    10    9    10    9    10    10    9    10    10    9    10    10    9    10    9    10    10    9    10    10    9    10    10    9    10    9    10    10    9    10    10    9    10    10    9    10    9    10    10    9    10    10    9    10    10    9    10    9    10    10    9    10    10    9    10    10    9    10    9    10    10    9    10    10    9    10    10    9    10    9    10    10    9    10    10    9    10    10    9    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    9    10    10    9    10    10    9    10    9    10    10    9    10    10    9    10    10    9    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    9    10    10    9    10    10    9    8    10    9    10    10    9    10    10    9    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    9    10    10    10    9    10    10    9    10    9    10    10    9    10    10    9    10    10    9    10    9    10    10    9    10    10    9    10    10    9    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    9    10    10    9    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    9    10    10    9    10    10    7    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    9    10    10    9    10    10    9    10    10    9    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    4    9    10    10    9    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    9    10    10    9    10    10    9    10    9    10    10    9    10    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    6    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    9    10    10    9    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    3    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    9    10    10    2    9    10    10    9    10    10    9    10    10    9    10    10    10    9    10    10    10    9    10    10    9    10    10    8    10    10    8    10    10    8    10    10    8   

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值