Questions
ThreadIntermediate3 min
How do you know when some process if blocking a UI thread?
Answer
How to Detect UI Thread Blocking
1. Strict Mode (Development)
StrictMode.setThreadPolicy( StrictMode.ThreadPolicy.Builder() .detectDiskReads() .detectDiskWrites() .detectNetwork() .penaltyLog() .build() )
2. ANR Detection
- If the UI thread is blocked for 5+ seconds, Android shows an ANR (Application Not Responding) dialog
- ANR traces are logged to
/data/anr/traces.txt
3. Using Choreographer
Choreographer.getInstance().postFrameCallback { frameTimeNanos -> val jank = (System.nanoTime() - frameTimeNanos) / 1_000_000 if (jank > 16) Log.w("Jank", "Frame took $jank ms") }
4. Profiler Tools
- Android Studio Profiler: Shows CPU usage and thread activity
- Systrace: Detailed trace of system and app threads
- Perfetto: Modern tracing tool for performance analysis
Key Signs of UI Thread Blocking:
- Dropped frames (jank)
- Unresponsive UI
- ANR dialogs
- StrictMode violations in Logcat
1:1 Mentorship
Get personalized guidance from a Google Developer Expert. Accelerate your career with dedicated support.
Personalized Learning Path
Mock Interviews & Feedback
Resume & Career Guidance
Share & Help Others
Help fellow developers prepare for interviews
Sharing helps the Android community grow 💚