在 Java/Android 中,有没有办法检查当前执行的代码行是否在后台线程上执行?我有一个我正在想象的小程序,它终于达到了完整的意大利面阶段……这是故意的,你看,因为这样……如果竞争对手拿到代码,他们“打开引擎盖,”看着它超过 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}
1 回答
慕妹3242003
TA贡献1824条经验 获得超6个赞
了解它的简单方法可能如下
boolean wasCalledFromBackgroundThread = (Thread.currentThread().getId() != 1);
后台线程没有 id 1(UI 线程有)。
添加回答
举报
0/150
提交
取消