设置Java线程的优先级 [英] Setting priority to Java's threads

查看:162
本文介绍了设置Java线程的优先级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在几个线程中运行的程序。主线程与其他线程共享一个对象,在main中我有一个调用:

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
}

我怀疑主要线程是饥饿的无法访问 obj 。如何提高主线程的优先级或默认情况下它是否已高于其他线程?

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?

推荐答案

你有一个Thread类中的setPriority()方法。

You have a setPriority() method in the Thread class.

检查这个javadoc

将线程优先级设置为最大值:

Setting thread priority to maximum:

public static void main(String args[]) {
    Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
    // Your main code.
}

这篇关于设置Java线程的优先级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆