androidengineers.Book a session
โ† All interview 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

Mark this when you can explain the answer in your own words.

Share & Help Others

Help fellow developers prepare for interviews

Sharing helps the Android community grow ๐Ÿ’š

Keep practising