Are there any different between Java thread priorities with pthread priorities?

649 views
Skip to first unread message

驼峰

unread,
Jul 13, 2011, 7:43:18 AM7/13/11
to android-ndk
Hi, All

I have one question regarding to the threading model of dalvik virtual
machine.

I created two threads by using Java layer APIs, each of the thread
procedure call one native API. In the native API, it get the thread
priority using pthread APIs. The native APIs always get the same
thread priority even if I call Thread.setPriority in Java layer.

My question is:

I think the Java thread should be native pthread. But why the thread
priority got by pthread API always be same (0)?
Are there any difference between the schedule policy by calling
Thread.setPriority and pthread_set_priority?

Thanks,
-Mike Guo

fadden

unread,
Jul 13, 2011, 1:37:38 PM7/13/11
to android-ndk
On Jul 13, 4:43 am, 驼峰 <guotuof...@gmail.com> wrote:
> I think the Java thread should be native pthread. But why the thread
> priority got by pthread API always be same (0)?
> Are there any difference between the schedule policy by calling
> Thread.setPriority and pthread_set_priority?

I don't see a pthread_set_priority() function. Are you using
pthread_setschedprio()?

Linux provides different ways to set thread priorities.
Thread.setPriority() currently turns into setpriority(), which sets
the "nice" value.

驼峰

unread,
Jul 13, 2011, 10:22:11 PM7/13/11
to android-ndk
sorry for I put wrong API name.
I actually mean pthread_setschedparam which can be used to change
native thread priority.

From Android source code perspective, Thread.setPriority() will
eventually call setpriority() with a value between -19 and 20.
On another hand, pthread has three schedule policy.
1. SCHED_OTHER. All thread has same priority (the priority value is 0)
2. SCHED_FIFO. The thread priority range is from 1 to 99.
3. SCHED_RR. Not sure the priority range.

My question is: which threading model does setpriority() used? The
value -19~20 is belong to which schedule policy?

Thanks,
-Mike Guo

驼峰

unread,
Jul 13, 2011, 10:22:20 PM7/13/11
to android-ndk

Angus Lees

unread,
Jul 14, 2011, 1:24:48 AM7/14/11
to andro...@googlegroups.com
The usual Linux default is SCHED_OTHER, and I would guess Android continues to use this.
(SCHED_RR and SCHED_FIFO can cause all the usual hard realtime priority problems when used carelessly, and I haven't heard of such problems arising in regular Android apps)

sched_setscheduler(2) manpage has a good description of the various policies, fwiw.

 - Gus


--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to andro...@googlegroups.com.
To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.


驼峰

unread,
Jul 14, 2011, 6:14:29 AM7/14/11
to android-ndk
If Android Dalvik virtual machine keep using SCHED_OTHER, all the
thread priority should be same. I doubt that Android customized
pthread thread model in DVM. So, I need someone to assure my
assumption is correct or not.

Thanks,
-Mike Guo

elio francesconi

unread,
Feb 21, 2012, 5:23:31 PM2/21/12
to andro...@googlegroups.com
So if the scheduler is  SCHED_OTHER there is not chance to change a thread priority?
Someone can confirm it?
Thanks
Elio

2011/7/14 驼峰 <guotu...@gmail.com>

Thanks,
-Mike Guo

Olivier Guilyardi

unread,
Feb 22, 2012, 12:32:03 AM2/22/12
to andro...@googlegroups.com
Le 02/21/2012 11:23 PM, elio francesconi a écrit :
> So if the scheduler is SCHED_OTHER there is not chance to change a
> thread priority?
> Someone can confirm it?

No, you can change thread priority but only to a higher nice value, that
is, a lower priority. AFAIK, this is Android specific. SCHED_OTHER on
vanilla Linux does allow normal processes to increase their priority.

--
Olivier

Reply all
Reply to author
Forward
0 new messages