设置 Java 线程的优先级
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1617963/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting priority to Java's threads
提问by Guy
I have a program that runs in a few threads. The main thread shares an object with the other threads and in the main I have a call to:
我有一个在几个线程中运行的程序。主线程与其他线程共享一个对象,在主线程中我有一个调用:
synchronized(obj){
do stuff
}
I have a suspicion that the main thread is starved and isn't getting access to obj
. How do I raise the priority of the main thread or is it already higher than the other threads by default?
我怀疑主线程被饿死并且无法访问obj
. 如何提高主线程的优先级,或者默认情况下它是否已经高于其他线程?
采纳答案by Macarse
You have a setPriority() method in the Thread class.
在 Thread 类中有一个 setPriority() 方法。
Check this javadoc.
检查这个 javadoc。
Setting thread priority to maximum:
将线程优先级设置为最大值:
public static void main(String args[]) {
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
// Your main code.
}
回答by djna
The series of articles hereindicate some complexities in the management of thread priorities on various platforms.
该系列文章在这里显示了一些在各种平台上的线程优先级的管理复杂性。
I wonder if your fundamental problem is simply that your worker threads are very CPU intensive and hence rarely reach a point where they would naturally "let go" of the processor (for example by doing some IO or sleeping.) If such is the case then you might include some calls to yield() in those workers, hence giving other Threads more of a chance.
我想知道您的基本问题是否仅仅是您的工作线程非常占用 CPU,因此很少达到他们自然会“放手”处理器的程度(例如通过执行一些 IO 或睡眠)。如果是这样,那么您可能会在这些工作线程中包含一些对 yield() 的调用,从而为其他线程提供更多机会。
回答by northpole
you can use the setPriority() method. For example:
您可以使用 setPriority() 方法。例如:
new MyThread("Foo").start();
Thread bar = new MyThread("Bar");
bar.setPriority( Thread.NORM_PRIORITY + 1 );
bar.start();
This gives bar the new priority which should quickly take over Foo
这给了 bar 新的优先级,它应该很快接管 Foo
Edit:
编辑:
To answer your comment, you can set the max priortiy using:
要回答您的评论,您可以使用以下方法设置最大优先级:
bar.setPriority( Thread.MAX_PRIORITY );
回答by Lorek
Increasing the main thread's priority the way
Macarsesays will probably work. However, you are relying on the platform's preemptive thread scheduler to work properly. You should instead call the Thread.yield()
static method in your worker threads when they are done with whatever important sections of code they are running. This is a good habit to get into even when you are using different levels of thread priority.
像Macarse所说的那样增加主线程的优先级
可能会起作用。但是,您依赖平台的抢占式线程调度程序才能正常工作。Thread.yield()
当工作线程完成它们正在运行的任何重要代码部分时,您应该在工作线程中调用静态方法。即使您使用不同级别的线程优先级,这也是一个很好的习惯。
回答by Sigi
As others already answered, you can indeed set a priority to a thread as follows:
正如其他人已经回答的那样,您确实可以为线程设置优先级,如下所示:
Thread.currentThread().setPriority(priority);
But please be aware, in your example, that thread priority has nothing to do in which order threads get access to a synchronized object. Java uses other criteria to give access. See for example this.
但是请注意,在您的示例中,线程优先级与线程访问同步对象的顺序无关。Java 使用其他标准来授予访问权限。参见例如这个。