安卓线程学习四 之 线程优先级和调度

一 问题思考

安卓开发,如果想设置线程优先级有两种方法:

1 Android sdk也提供一个设置线程优先级的方法

2 Thread.java里面提供了设置线程优先级的方法

这两个方法有什么区别,应该选择使用哪一个呢?

二 线程优先级的原理

2.1. android.os.process.java设置线程优先级源码分析

/**
     * Set the priority of the calling thread, based on Linux priorities.  See
     * {@link #setThreadPriority(int, int)} for more information.
     * 
     * @param priority A Linux priority level, from -20 for highest scheduling
     * priority to 19 for lowest scheduling priority.
     * 
     * @throws IllegalArgumentException Throws IllegalArgumentException if
     * <var>tid</var> does not exist.
     * @throws SecurityException Throws SecurityException if your process does
     * not have permission to modify the given thread, or to use the given
     * priority.
     * 
     * @see #setThreadPriority(int, int)
     */
    public static final native void setThreadPriority(int priority)
            throws IllegalArgumentException, SecurityException;

注释的意思大致是这里设置优先级是基于linux线程优先级的实现。这是一个jni方法,对应实现文件在android_util_process.cpp文件:

tatic const JNINativeMethod methods[] = {
    {
  "getUidForName",       "(Ljava/lang/String;)I", (void*)android_os_Process_getUidForName},
    {
  "getGidForName",       "(Ljava/lang/String;)I", (void*)android_os_Process_getGidForName},
    {
  "setThreadPriority",   "(II)V", (void*)android_os_Pr
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值