在Java / Android中,是否可以检查当前正在执行的代码行是否在后台线程上执行?
我正在幻想一个lil'程序,该程序终于到达了全意大利面条阶段...您知道,这是有意的,因为这样...如果竞争对手获得了代码,并且他们“打开引擎盖”,看着它超过20秒钟,他们的头发会着火,然后尖叫着逃跑……但是现在,即使我感到困惑,我也需要以某种方式检查这种情况。
附件A:
// can be called from 1,067 places... some of which are background threads.
public void startDoingAFunDance(String caller, int wobbleIntensity, int spineAngle, int feetSeparationInInches) {
if (!validCallersForFunDance.contains(caller)) {
Log.i("XXX", "Caller not allowed.");
return;
}
boolean wasCalledFromBackgroundThread = // ? < what to put here > ?
Log.i("XXX", "Was startDoingAFunDance() called from a background thread? And the answer is: " + wasCalledFromBackgroundThread);
// classified
}
答案 0 :(得分:2)
一个简单的方法可能是以下内容
boolean wasCalledFromBackgroundThread = (Thread.currentThread().getId() != 1);
后台线程没有ID 1(UI线程具有ID)。