Android线程优先级对性能的影响

android可以通过调用thread对象的setPriority方法为线程设置优先级,优先级只能取1~10之间的整数。优先级越大意味着线程优先级越高,在大量并发时享有更多的cpu执行时间。

android主线程默认优先级是5,我们可以按照如下代码来测试不同优先级对性能的影响。

      public static void testThreadPriority() {

        for (int i = 0; i < 30; i++) {
            final int ii = i;
            new Thread() {
                @Override
                public void run() {
                    super.run();
                    compute(Thread.currentThread(), ii % 9 + 1);
                }
            }.start();
        }
        int uiPriority = Thread.currentThread().getPriority();
        //uiPriority = 10;// 提高ui线程优先级会减少计算时间
        long l = compute(Thread.currentThread(), uiPriority);
        Log.d("ThreadPriority", "主线程Priority: " + uiPriority + " 耗时 " + l);
    }

    /**
     * 设置线程优先级,然后进行大量的字符拼接计算
     *
     * @param thread
     * @param priority
     * @return
     */
    private static long compute(Thread thread, int priority) {
        Log.d("ThreadPriority", "启动了。。。" + priority);
        //设置当前线程优先级
        thread.setPriority(priority);
        long s = System.currentTimeMillis();
        String s1 = "";
        for (int j = 0; j < 400000; j++) {
            s1 += j;
            if (s1.length() > 500) {
                s1 = "";
            }
        }
        long spend = (System.currentTimeMillis() - s);
        Log.d("ThreadPriority", Thread.currentThread().getPriority() + " " + spend);

        return spend;
    }

在小米6 6G版 android8.0手机上输出如下:可以看到优先级越高通常来说需要的时间越短,由于主线程默认的优先级是5,所以计算时间和其他优先级同样是5的线程所花费的时间基本一致。多次运行结果也印证了这个观点。

12-10 14:52:44.570 12906-12948/? D/ThreadPriority: 启动了。。。1
12-10 14:52:44.571 12906-12950/? D/ThreadPriority: 启动了。。。3
12-10 14:52:44.571 12906-12949/? D/ThreadPriority: 启动了。。。2
12-10 14:52:44.572 12906-12952/? D/ThreadPriority: 启动了。。。5
12-10 14:52:44.573 12906-12953/? D/ThreadPriority: 启动了。。。6
12-10 14:52:44.573 12906-12951/? D/ThreadPriority: 启动了。。。4
12-10 14:52:44.575 12906-12954/? D/ThreadPriority: 启动了。。。7
12-10 14:52:44.575 12906-12955/? D/ThreadPriority: 启动了。。。8
12-10 14:52:44.576 12906-12956/? D/ThreadPriority: 启动了。。。9
12-10 14:52:44.578 12906-12957/? D/ThreadPriority: 启动了。。。1
12-10 14:52:44.601 12906-12958/? D/ThreadPriority: 启动了。。。2
12-10 14:52:44.626 12906-12960/? D/ThreadPriority: 启动了。。。3
12-10 14:52:44.629 12906-12961/? D/ThreadPriority: 启动了。。。4
12-10 14:52:44.639 12906-12962/? D/ThreadPriority: 启动了。。。5
12-10 14:52:44.641 12906-12963/? D/ThreadPriority: 启动了。。。6
12-10 14:52:44.652 12906-12964/? D/ThreadPriority: 启动了。。。7
12-10 14:52:44.653 12906-12965/? D/ThreadPriority: 启动了。。。8
12-10 14:52:44.658 12906-12967/? D/ThreadPriority: 启动了。。。9
12-10 14:52:44.688 12906-12968/? D/ThreadPriority: 启动了。。。1
12-10 14:52:44.690 12906-12969/? D/ThreadPriority: 启动了。。。2
12-10 14:52:44.699 12906-12970/? D/ThreadPriority: 启动了。。。3
12-10 14:52:44.704 12906-12971/? D/ThreadPriority: 启动了。。。4
12-10 14:52:44.779 12906-12972/? D/ThreadPriority: 启动了。。。5
12-10 14:52:44.782 12906-12973/? D/ThreadPriority: 启动了。。。6
12-10 14:52:44.794 12906-12974/? D/ThreadPriority: 启动了。。。7
12-10 14:52:44.809 12906-12977/? D/ThreadPriority: 启动了。。。8
12-10 14:52:44.812 12906-12978/? D/ThreadPriority: 启动了。。。9
12-10 14:52:44.814 12906-12979/? D/ThreadPriority: 启动了。。。1
12-10 14:52:44.835 12906-12906/? D/ThreadPriority: 启动了。。。5
12-10 14:52:44.850 12906-12980/? D/ThreadPriority: 启动了。。。2
12-10 14:52:44.936 12906-12981/? D/ThreadPriority: 启动了。。。3
12-10 14:52:50.403 12906-12978/com.wanjian D/ThreadPriority: 9 5590
12-10 14:52:50.582 12906-12956/com.wanjian D/ThreadPriority: 9 6006
12-10 14:52:50.689 12906-12955/com.wanjian D/ThreadPriority: 8 6114
12-10 14:52:51.162 12906-12977/com.wanjian D/ThreadPriority: 8 6352
12-10 14:52:51.270 12906-12954/com.wanjian D/ThreadPriority: 7 6695
12-10 14:52:51.373 12906-12967/com.wanjian D/ThreadPriority: 9 6715
12-10 14:52:51.424 12906-12965/com.wanjian D/ThreadPriority: 8 6771
12-10 14:52:52.113 12906-12964/com.wanjian D/ThreadPriority: 7 7460
12-10 14:52:52.231 12906-12963/com.wanjian D/ThreadPriority: 6 7589
12-10 14:52:52.334 12906-12974/com.wanjian D/ThreadPriority: 7 7540
12-10 14:52:52.391 12906-12952/com.wanjian D/ThreadPriority: 5 7819
12-10 14:52:52.650 12906-12973/com.wanjian D/ThreadPriority: 6 7868
12-10 14:52:52.778 12906-12972/com.wanjian D/ThreadPriority: 5 7998
12-10 14:52:53.175 12906-12962/com.wanjian D/ThreadPriority: 5 8536
12-10 14:52:53.505 12906-12953/com.wanjian D/ThreadPriority: 6 8932
12-10 14:52:53.551 12906-12906/com.wanjian D/ThreadPriority: 5 8716
    主线程Priority: 5 耗时 8716
12-10 14:52:53.780 12906-12970/com.wanjian D/ThreadPriority: 3 9080
12-10 14:52:54.011 12906-12971/com.wanjian D/ThreadPriority: 4 9306
12-10 14:52:54.403 12906-12960/com.wanjian D/ThreadPriority: 3 9776
12-10 14:52:54.405 12906-12980/com.wanjian D/ThreadPriority: 2 9449
12-10 14:52:54.445 12906-12961/com.wanjian D/ThreadPriority: 4 9815
12-10 14:52:54.494 12906-12968/com.wanjian D/ThreadPriority: 1 9805
12-10 14:52:54.589 12906-12981/com.wanjian D/ThreadPriority: 3 9653
12-10 14:52:54.732 12906-12957/com.wanjian D/ThreadPriority: 1 10136
12-10 14:52:54.803 12906-12969/com.wanjian D/ThreadPriority: 2 10109
12-10 14:52:54.831 12906-12950/com.wanjian D/ThreadPriority: 3 10260
12-10 14:52:54.846 12906-12951/com.wanjian D/ThreadPriority: 4 10272
12-10 14:52:54.929 12906-12958/com.wanjian D/ThreadPriority: 2 10326
12-10 14:52:54.948 12906-12979/com.wanjian D/ThreadPriority: 1 10122
12-10 14:52:55.011 12906-12948/com.wanjian D/ThreadPriority: 1 10440
12-10 14:52:55.110 12906-12949/com.wanjian D/ThreadPriority: 2 10539

当把主线程优先级改成最高时(把这句注释开启时uiPriority = 10;),输出如下:

12-10 14:58:11.241 13666-13692/com.wanjian D/ThreadPriority: 启动了。。。1
12-10 14:58:11.241 13666-13693/com.wanjian D/ThreadPriority: 启动了。。。2
12-10 14:58:11.241 13666-13694/com.wanjian D/ThreadPriority: 启动了。。。3
12-10 14:58:11.241 13666-13695/com.wanjian D/ThreadPriority: 启动了。。。4
12-10 14:58:11.242 13666-13697/com.wanjian D/ThreadPriority: 启动了。。。6
12-10 14:58:11.242 13666-13696/com.wanjian D/ThreadPriority: 启动了。。。5
12-10 14:58:11.243 13666-13698/com.wanjian D/ThreadPriority: 启动了。。。7
12-10 14:58:11.247 13666-13699/com.wanjian D/ThreadPriority: 启动了。。。8
12-10 14:58:11.247 13666-13700/com.wanjian D/ThreadPriority: 启动了。。。9
12-10 14:58:11.256 13666-13702/com.wanjian D/ThreadPriority: 启动了。。。2
12-10 14:58:11.266 13666-13701/com.wanjian D/ThreadPriority: 启动了。。。1
12-10 14:58:11.274 13666-13703/com.wanjian D/ThreadPriority: 启动了。。。3
12-10 14:58:11.278 13666-13704/com.wanjian D/ThreadPriority: 启动了。。。4
12-10 14:58:11.280 13666-13705/com.wanjian D/ThreadPriority: 启动了。。。5
12-10 14:58:11.282 13666-13706/com.wanjian D/ThreadPriority: 启动了。。。6
12-10 14:58:11.306 13666-13707/com.wanjian D/ThreadPriority: 启动了。。。7
12-10 14:58:11.309 13666-13708/com.wanjian D/ThreadPriority: 启动了。。。8
12-10 14:58:11.317 13666-13709/com.wanjian D/ThreadPriority: 启动了。。。9
12-10 14:58:11.319 13666-13710/com.wanjian D/ThreadPriority: 启动了。。。1
12-10 14:58:11.325 13666-13711/com.wanjian D/ThreadPriority: 启动了。。。2
12-10 14:58:11.327 13666-13712/com.wanjian D/ThreadPriority: 启动了。。。3
12-10 14:58:11.336 13666-13713/com.wanjian D/ThreadPriority: 启动了。。。4
12-10 14:58:11.341 13666-13714/com.wanjian D/ThreadPriority: 启动了。。。5
12-10 14:58:11.364 13666-13715/com.wanjian D/ThreadPriority: 启动了。。。6
12-10 14:58:11.388 13666-13717/com.wanjian D/ThreadPriority: 启动了。。。8
12-10 14:58:11.388 13666-13716/com.wanjian D/ThreadPriority: 启动了。。。7
12-10 14:58:11.405 13666-13718/com.wanjian D/ThreadPriority: 启动了。。。9
12-10 14:58:11.415 13666-13719/com.wanjian D/ThreadPriority: 启动了。。。1
12-10 14:58:11.417 13666-13666/com.wanjian D/ThreadPriority: 启动了。。。10
12-10 14:58:11.434 13666-13720/com.wanjian D/ThreadPriority: 启动了。。。2
12-10 14:58:11.487 13666-13721/com.wanjian D/ThreadPriority: 启动了。。。3
12-10 14:58:17.183 13666-13709/com.wanjian D/ThreadPriority: 9 5866
12-10 14:58:17.622 13666-13699/com.wanjian D/ThreadPriority: 8 6375
12-10 14:58:17.980 13666-13718/com.wanjian D/ThreadPriority: 9 6575
12-10 14:58:18.169 13666-13666/com.wanjian D/ThreadPriority: 10 6743
    主线程Priority: 10 耗时 6743
12-10 14:58:18.487 13666-13700/com.wanjian D/ThreadPriority: 9 7238
12-10 14:58:18.534 13666-13706/com.wanjian D/ThreadPriority: 6 7251
12-10 14:58:18.561 13666-13707/com.wanjian D/ThreadPriority: 7 7255
12-10 14:58:18.776 13666-13717/com.wanjian D/ThreadPriority: 8 7388
12-10 14:58:18.786 13666-13715/com.wanjian D/ThreadPriority: 6 7399
12-10 14:58:18.823 13666-13716/com.wanjian D/ThreadPriority: 7 7435
12-10 14:58:19.103 13666-13696/com.wanjian D/ThreadPriority: 5 7861
12-10 14:58:19.285 13666-13697/com.wanjian D/ThreadPriority: 6 8043
12-10 14:58:19.565 13666-13708/com.wanjian D/ThreadPriority: 8 8253
12-10 14:58:19.771 13666-13714/com.wanjian D/ThreadPriority: 5 8430
12-10 14:58:19.825 13666-13705/com.wanjian D/ThreadPriority: 5 8544
12-10 14:58:19.834 13666-13698/com.wanjian D/ThreadPriority: 7 8591
12-10 14:58:20.122 13666-13712/com.wanjian D/ThreadPriority: 3 8795
12-10 14:58:20.718 13666-13694/com.wanjian D/ThreadPriority: 3 9477
12-10 14:58:20.936 13666-13720/com.wanjian D/ThreadPriority: 2 9501
12-10 14:58:20.957 13666-13703/com.wanjian D/ThreadPriority: 3 9681
12-10 14:58:21.167 13666-13713/com.wanjian D/ThreadPriority: 4 9831
12-10 14:58:21.207 13666-13702/com.wanjian D/ThreadPriority: 2 9951
12-10 14:58:21.282 13666-13704/com.wanjian D/ThreadPriority: 4 10004
12-10 14:58:21.304 13666-13721/com.wanjian D/ThreadPriority: 3 9815
12-10 14:58:21.326 13666-13693/com.wanjian D/ThreadPriority: 2 10084
12-10 14:58:21.401 13666-13701/com.wanjian D/ThreadPriority: 1 10135
12-10 14:58:21.447 13666-13692/com.wanjian D/ThreadPriority: 1 10206
12-10 14:58:21.462 13666-13711/com.wanjian D/ThreadPriority: 2 10136
12-10 14:58:21.502 13666-13695/com.wanjian D/ThreadPriority: 4 10261
12-10 14:58:21.546 13666-13710/com.wanjian D/ThreadPriority: 1 10227
12-10 14:58:21.561 13666-13719/com.wanjian D/ThreadPriority: 1 10145

所以我们是否可以尝试为主线程设置更高的优先级来提高主线程的流畅度?

我们还可调用Process.setThreadPriority(value);为当前线程设置优先级,取值-19~19,数值越小优先级越高,可以通过Process.getThreadPriority(Process.myTid())获取当前线程的优先级,主线程的默认优先级-19,其他线程默认是0,主线程没法提高了,但我们可以降低其他线程的优先级,从而实现主线程获取更多的cpu执行时间。

我们把代码改成如下:把主线程优先级提高到最高,其他线程降低到最低



    public static void testThreadPriority() {

        for (int i = 0; i < 30; i++) {
            new Thread() {
                @Override
                public void run() {
                    super.run();
                    Process.setThreadPriority(19);
                    Thread.currentThread().setPriority(1);
                    compute();
                }
            }.start();
        }

        //Process.setThreadPriority(-19);
        Thread.currentThread().setPriority(10);
        long l = compute();
        Log.d("ThreadPriority", "主线程耗时 " + l);
    }

    /**
     * 进行大量的字符拼接计算
     *
     * @return
     */
    private static long compute() {
        Log.d("ThreadPriority", "启动了。。。");
        long s = System.currentTimeMillis();
        String s1 = "";
        for (int j = 0; j < 400000; j++) {
            s1 += j;
            if (s1.length() > 500) {
                s1 = "";
            }
        }
        long spend = (System.currentTimeMillis() - s);
        Log.d("ThreadPriority", "" + spend);

        return spend;
    }

输出:可以看到主线程要快很多

12-10 15:50:28.088 19857-19877/com.wanjian D/ThreadPriority: 启动了。。。
12-10 15:50:28.088 19857-19878/com.wanjian D/ThreadPriority: 启动了。。。
12-10 15:50:28.089 19857-19880/com.wanjian D/ThreadPriority: 启动了。。。
12-10 15:50:28.089 19857-19879/com.wanjian D/ThreadPriority: 启动了。。。
12-10 15:50:28.089 19857-19881/com.wanjian D/ThreadPriority: 启动了。。。
12-10 15:50:28.090 19857-19882/com.wanjian D/ThreadPriority: 启动了。。。
12-10 15:50:28.091 19857-19883/com.wanjian D/ThreadPriority: 启动了。。。
12-10 15:50:28.093 19857-19884/com.wanjian D/ThreadPriority: 启动了。。。
12-10 15:50:28.095 19857-19885/com.wanjian D/ThreadPriority: 启动了。。。
12-10 15:50:28.096 19857-19886/com.wanjian D/ThreadPriority: 启动了。。。
12-10 15:50:28.098 19857-19887/com.wanjian D/ThreadPriority: 启动了。。。
12-10 15:50:28.100 19857-19888/com.wanjian D/ThreadPriority: 启动了。。。
12-10 15:50:28.106 19857-19889/com.wanjian D/ThreadPriority: 启动了。。。
12-10 15:50:28.107 19857-19890/com.wanjian D/ThreadPriority: 启动了。。。
12-10 15:50:28.114 19857-19891/com.wanjian D/ThreadPriority: 启动了。。。
12-10 15:50:28.115 19857-19892/com.wanjian D/ThreadPriority: 启动了。。。
12-10 15:50:28.120 19857-19893/com.wanjian D/ThreadPriority: 启动了。。。
12-10 15:50:28.122 19857-19894/com.wanjian D/ThreadPriority: 启动了。。。
12-10 15:50:28.126 19857-19895/com.wanjian D/ThreadPriority: 启动了。。。
12-10 15:50:28.131 19857-19896/com.wanjian D/ThreadPriority: 启动了。。。
12-10 15:50:28.131 19857-19897/com.wanjian D/ThreadPriority: 启动了。。。
12-10 15:50:28.134 19857-19898/com.wanjian D/ThreadPriority: 启动了。。。
12-10 15:50:28.137 19857-19899/com.wanjian D/ThreadPriority: 启动了。。。
12-10 15:50:28.139 19857-19900/com.wanjian D/ThreadPriority: 启动了。。。
12-10 15:50:28.144 19857-19901/com.wanjian D/ThreadPriority: 启动了。。。
12-10 15:50:28.148 19857-19902/com.wanjian D/ThreadPriority: 启动了。。。
12-10 15:50:28.206 19857-19904/com.wanjian D/ThreadPriority: 启动了。。。
12-10 15:50:28.216 19857-19903/com.wanjian D/ThreadPriority: 启动了。。。
12-10 15:50:28.222 19857-19857/com.wanjian D/ThreadPriority: 启动了。。。
12-10 15:50:28.234 19857-19905/com.wanjian D/ThreadPriority: 启动了。。。
12-10 15:50:28.235 19857-19906/com.wanjian D/ThreadPriority: 启动了。。。
12-10 15:50:33.377 19857-19857/com.wanjian D/ThreadPriority: 主线程耗时 5155
12-10 15:50:34.816 19857-19891/com.wanjian D/ThreadPriority: 6702
12-10 15:50:36.111 19857-19905/com.wanjian D/ThreadPriority: 7877
12-10 15:50:36.373 19857-19890/com.wanjian D/ThreadPriority: 8266
12-10 15:50:36.904 19857-19901/com.wanjian D/ThreadPriority: 8760
12-10 15:50:36.966 19857-19903/com.wanjian D/ThreadPriority: 8750
12-10 15:50:37.077 19857-19904/com.wanjian D/ThreadPriority: 8871
12-10 15:50:37.156 19857-19897/com.wanjian D/ThreadPriority: 9025
12-10 15:50:37.292 19857-19883/com.wanjian D/ThreadPriority: 9201
12-10 15:50:37.450 19857-19888/com.wanjian D/ThreadPriority: 9350
12-10 15:50:37.536 19857-19902/com.wanjian D/ThreadPriority: 9388
12-10 15:50:37.602 19857-19879/com.wanjian D/ThreadPriority: 9513
12-10 15:50:37.744 19857-19896/com.wanjian D/ThreadPriority: 9613
12-10 15:50:37.769 19857-19889/com.wanjian D/ThreadPriority: 9662
12-10 15:50:37.789 19857-19887/com.wanjian D/ThreadPriority: 9691
12-10 15:50:37.800 19857-19885/com.wanjian D/ThreadPriority: 9705
12-10 15:50:37.900 19857-19892/com.wanjian D/ThreadPriority: 9785
12-10 15:50:37.934 19857-19894/com.wanjian D/ThreadPriority: 9812
12-10 15:50:37.968 19857-19882/com.wanjian D/ThreadPriority: 9878
12-10 15:50:37.982 19857-19898/com.wanjian D/ThreadPriority: 9848
12-10 15:50:38.023 19857-19893/com.wanjian D/ThreadPriority: 9903
12-10 15:50:38.046 19857-19900/com.wanjian D/ThreadPriority: 9907
12-10 15:50:38.067 19857-19880/com.wanjian D/ThreadPriority: 9978
12-10 15:50:38.104 19857-19899/com.wanjian D/ThreadPriority: 9967
12-10 15:50:38.214 19857-19878/com.wanjian D/ThreadPriority: 10125
12-10 15:50:38.225 19857-19906/com.wanjian D/ThreadPriority: 9989
12-10 15:50:38.266 19857-19877/com.wanjian D/ThreadPriority: 10178
12-10 15:50:38.373 19857-19881/com.wanjian D/ThreadPriority: 10283
12-10 15:50:38.411 19857-19886/com.wanjian D/ThreadPriority: 10315
12-10 15:50:38.492 19857-19895/com.wanjian D/ThreadPriority: 10366
12-10 15:50:38.537 19857-19884/com.wanjian D/ThreadPriority: 10444

你可能感兴趣的:(安卓,ui)