好吧,让我们看一下源代码:
2947行:
////////////////////////////////////////////////////////////////////////////////// thread priority support// Note: Normal Linux applications are run with SCHED_OTHER policy. SCHED_OTHER// only supports dynamic priority, static priority must be zero. For real-time// applications, Linux supports SCHED_RR which allows static priority (1-99).// However, for large multi-threaded applications, SCHED_RR is not only slower// than SCHED_OTHER, but also very unstable (my volano tests hang hard 4 out// of 5 runs - Sep 2005).//// The following pre actually changes the niceness of kernel-thread/LWP. It// has an assumption that setpriority() only modifies one kernel-thread/LWP,// not the entire user process, and user level threads are 1:1 mapped to kernel// threads. It has always been the case, but could change in the future. For// this reason, the pre should not be used as default (ThreadPriorityPolicy=0).// It is only used when ThreadPriorityPolicy=1 and requires root privilege.
…
第2982行:
static int prio_init() { if (ThreadPriorityPolicy == 1) { // only root can raise thread priority. Don't allow ThreadPriorityPolicy=1 // if effective uid is not root. Perhaps, a more elegant way of doing // this is to test CAP_SYS_NICE capability, but that will require libcap.so if (geteuid() != 0) { if (!FLAG_IS_DEFAULT(ThreadPriorityPolicy)) { warning("-XX:ThreadPriorityPolicy requires root privilege on Linux"); } ThreadPriorityPolicy = 0; } } return 0; }…
第2997行:
OSReturn os::set_native_priority(Thread* thread, int newpri) { if ( !UseThreadPriorities || ThreadPriorityPolicy == 0 ) return OS_OK; int ret = setpriority(PRIO_PROCESS, thread->osthread()->thread_id(), newpri); return (ret == 0) ? OS_OK : OS_ERR;}所以!至少在Sun
Java和Linux上,除非您已经完成
-XX:ThreadPriorityPolicy并且似乎需要root用户,否则您将看不到线程优先级。



