🚀 Enrollments Open for 1:1 Mentorship Scheduled as per your availability 💚Book Now
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

Want to go deeper?

Read our full guides and blog posts on Thread and related Android topics.

Want to master these concepts?

Join our live cohorts and build production-ready Android apps.

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

Limited slots available each month