when dispatching ,JVM will select the thread with the highest priority , but when you change a thread priority ,it will not interrupt the running thread. when the time-slice arrived ,the running thread will suspend and the thread with the hignest priority will run.

解决方案 »

  1.   

    to fawnsh() :你所说的是在time-slice的操作系统中,但是如果是在坚持优先级原则的操作系统中呢???
      

  2.   

    in JAVA ,the JVM use time-slice to implement thread library.
      

  3.   

    哦,那我糊涂了,有道题目是这样的
    22. What can cause a thread to stop executing?
    A. The program exits via a call to exit(0).
    B. The priority of another thread is increased.
    C. A call to the stop method of the Thread class.
    D. A call to the halt method of the Thread class.
    照你的理解应该是A.B喽可是答案是A.B.C.啊另外,我记得线程的调度是平台相关的啊,好像和你所说的不一样哦
      

  4.   

    please read 'The Java Virtual Machine Specification'
    url:http://java.sun.com/docs/books/vmspec/html/Concepts.doc.html#24465
    some abstract following:
    Threads may be supported by having many hardware processors, by time-slicing a single hardware processor, or by time-slicing many hardware processors. 
      

  5.   

    to pengji(彭乃超)
    in Java Language Specification
    'When there is competition for processing resources, threads with higher priority are generally executed in preference to threads with lower priority. Such preference is not, however, a guarantee that the highest priority thread will always be running'
      

  6.   

    i think choice B is not right,if the time-slice is not timeout ,the running thread will not stop run,when the time-slice is timeout the execution will be change to the thread with higher priority,what do you think about it?
      

  7.   

    我认为如果你的一个线程的优先级是3,如果在它运行其间改变了另一个阻塞线程的优先级是它高于自己,那么它自己将被(停止还是阻塞不记得了!),而那个线程将被运行!
    to:fawnsh()你说的我看不大懂不过大致意思我知道了,就是优先级别高的线程将被一直运行,对吗?! 
      

  8.   

    no.
    Such preference is 
    not, however,a guarantee
    ......................... 
    that the highest priority thread will always be running' 
      

  9.   

    我认为是ABC从Java的优先级定义中,JVM应该屏蔽底层实现细节,并保证priority
    起作用,所以当另一个Thread的priority上升之后,这一行为会“加速”
    使本Thread“更倾向于”被“挂起”(但并不保证何时“挂起”)但从目前不同JVM的不同实现来看,priority调度的支持不太好,
    使用了Java priority调度机制的代码实际上不是“跨平台”的。
    所以Joshua Bloch大侠在"Effective Java"一书中建议最好不要使用
    Java中的priority调度机制(基于类似原因,另外他还建议不要用ThreadGroup)。
      

  10.   

    这个问题很麻烦,涉及的太多了。
    实际上线程的调度是和操作系统相关的,这一点上java并不能做到完全的跨平台。在一个协作式的线程调度平台上,增加一个线程的优先级对于线程调度可能没有任何作用,但是对于抢占式调度平台而言,增加一个线程的优先级的效果就是立杆见影了。典型的协作式调度平台就是win3.2到win98,nt和98以上版本的windows基本上都是两种结合的(即协作式和抢占式的混合),而solaris的绿色线程模型则是完全的抢占式的。java中的优先级有十种之多,但是目前没有那个操作系统有这么多的优先级,大多就是三种,因此,如果是将优先级提高一级,在抢占式线程平台下也不一定有效果,因此目前使用线程优先级的策略是尽量不用,而是通过在必要的时候使用sleep或者wait方法阻塞你要抢占的其它线程,执行完后再唤醒他们。如果必须用,那么只用三种,普通级、最高级和最低级。使用十级优先级别在大部分情况下没有任何意义。