What's the difference between Thread.setPriority() and android.os.Process.setThreadPriority()

前端 未结 3 442
臣服心动
臣服心动 2020-12-22 23:45

If I have code like:

Runnable r = ...;

Thread  thread = new Thread(r);
thread.setPriority((Thread.MAX_PRIORITY + Thread.NORM_PRIORITY) / 2);
相关标签:
3条回答
  • 2020-12-22 23:58

    The current Dalvik implementation seems to map Java Threads one by one to the underlying linux system PTHREADs like you say. All Threads of all apps belong to the same thread group on the system, so every Thread competes with all Threads of all apps.

    So currently Thread.setPriority should actually do the same thing as Process.setThreadPriority, using the smaller Java priority scale. The mapping of priorities is defined in kNiceValues at vm/Thread.c

    0 讨论(0)
  • 2020-12-23 00:03

    I would rely on thread.setPriority(). The android.os.Process.setThreadPriority names a real thread priority of the underliying linux OS. However, those could, but doesn't need to map to Dalvik / Java VM threads, as the VM could do threading on its own means or use system threads or a combination of both. Raising the system priority would more likely result in prioritizing your application in favor of others, if not restricted by android security constraints, but not guarantee prioritizing your current Java Thread in favor of other Java Threads in your application.

    0 讨论(0)
  • 2020-12-23 00:05

    Google uses

    Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
    

    in Volley's Network Dispatcher thread, so I would believe that using Process.setThreadPriority() is the way to go.

    0 讨论(0)
提交回复
热议问题