【发布时间】:2023-03-17 22:53:01
【问题描述】:
我有以下代码onCreate()
Log.d(TAG, "Setting priority background");
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
Log.d(TAG, Thread.currentThread().getId() + ": " + Thread.currentThread().getPriority());
Log.d(TAG, Process.getThreadPriority(Process.myTid()) + " - myTid() " + Process.myTid() + " Thread.getId() = " + Thread.currentThread().getId());
// start a thread here
Thread thread = new Thread(() -> {
Log.d(TAG, " In new thread");
Log.d(TAG, Thread.currentThread().getId() + ": " + Thread.currentThread().getPriority());
Log.d(TAG, Process.getThreadPriority(Process.myTid()) + " - myTid() " + Process.myTid() + "Thread.getId() = " + Thread.currentThread().getId());
}
输出是:
Setting priority background
1: 5
10 - myTid() 8798 Thread.getId() = 1
In new thread
7534: 10
-8 - myTid() 8819 Thread.getId() = 7534
谁能解释一下:
1) 即使我设置了setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);,下一行中的日志也会显示优先级5。为什么?
2) 第二个线程的优先级输出中的-8 来自哪里?
3) 新线程的10 也是从哪里来的?
更新:
如果我删除行 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
输出保持不变
【问题讨论】:
标签: java android multithreading linux-kernel android-threading