我们是无法具体决定哪个现在来运行,优先级高只是意味着它得到的CPU的时间段多一些,并不代表它能优先运行。
解决方案 »
- 关于mysql的一些小问题
- 如何在model或则control里定义list放到session()里
- 关于synchronized的顺序问题
- 急,jfreechart显示横坐标字省略号,如何解决
- 缴费金额必须填写,不能为0,但可以为负数,有没有这个FORM的验证或是java代码
- microsoft.sqlserver.jdbc
- resultset.updateString ()错 请高手指点 在运行的时候报“对指结果集的无效操作” 错 不知道何哉
- 关于JAVA文件,读取格式的问题
- 我原来的J2SDK 1.4.2_05有问题,现在重下一个J2SDK1.4.2-WINDOWS-I586,可不可以啊
- 急急急,求MD5算法,大侠请进,在线等候
- 需要中英文对应的文章
- java sdk中的例子中 *.java 用写字板打开不能自动换行,不知它用什么文本编辑器?
文章的标题是Dispelling Java programming language myths Myth 5: Waiting threads are awakened in priority order
When writing multithreaded code, often the situation arises where you have more than one thread waiting on an event. This occurs when more than one thread calls wait inside a synchronized method or block that is locked on the same object. Waiting threads are awakened when another thread calls notify or notifyAll from within a synchronized method or block that is locked on the same object. The notify call wakes up only one thread. Therefore, if more than one thread is waiting, there will be no contention for the lock. The notifyAll call, on the other hand, wakes up all waiting threads to compete for the lock. However, only one thread will get the lock; the others will block. When multiple threads are waiting, the question is which thread runs after a call to notify or notifyAll? Many programmers incorrectly assume that there is a predefined order to how threads are awakened. Some think that the highest priority thread is awakened first, while others might think it is the thread that has been waiting the longest. Unfortunately, neither assumption is true. In these situations, the thread that is awakened is undefined. It might be the thread with the highest priority or the thread that has been waiting the longest, but there is no guarantee that it will be. The priority of a thread does not determine whether it is notified (in the case of using the notify method) or in what order multiple threads are notified (in the case of using the notifyAll method). Therefore, you should never make assumptions about the order in which threads are awakened as a result of calls to these methods. In addition, you should never make assumptions about the scheduling of a thread during preemption. Thread scheduling is implementation-dependent and varies by platform. It is unwise to make this type of assumption if your code is to be portable. In addition, the notifyAll method, like the notify method, does not provide a way to specify the order in which waiting threads are notified. The order depends on the Java virtual machine, and no guarantees are made beyond the fact that all waiting threads are awakened. This behavior presents a problem when you need to notify multiple threads in a particular order. There are two ways to achieve a controlled order of awakened threads: Use the specific notification pattern
Use a VM implemented with the Real-Time Specification for Java (RTSJ)
Specific notification pattern
This pattern, developed by Tom Cargill, specifies how you can control the order of threads to be awakened from a call to notify and notifyAll. This is accomplished through a separate lock object for each thread, or set of threads, that needs to be notified together. This enables notification to happen in a defined order to a specific lock object and thus to its associated thread or threads. The execution overhead of this pattern is minimal if implemented properly. There is, however, some added code complexity. This added complexity can be displaced with the additional control this pattern gives you. You should consider implementing this pattern if you have the need to control the notification order of multiple threads. RTSJ
The RTSJ changes the standard behavior of certain Java semantics. One of these semantics is ensuring that waiting threads are queued in priority order. Therefore, when one or more threads are waiting and a call is made to notify or notifyAll, the thread with the highest priority executes first. The others must wait.Generally, it is not recommended to use a real-time implementation for anything but real-time programs. There are various trade-offs that are made to enable the Java programming language for real-time programming. One overriding principle in the creation of the RTSJ is that timeliness takes precedence over execution speed.
用其它的办法来实现你的要求吧。
可是java总因该和平台的操作系统调度挂勾吧,jvm不会把将要运行的线程放入操作系统的就绪队列吗,而实时强占方式会使就绪队列的线程优先级提高,当高于正在运行的线程时,会讲它阻塞并放入就绪队列!!
这些都因该是操作系统所做的呀??
关于线程的见解蛮多的!!
请问这样行吗?