0

Is the Android thread scheduler guaranteed to prioritize the main UI thread over all other user (worker/background) threads, or might user threads interrupt the main thread? My goal is to avoid race conditions without over-synchronizing my Java/Kotlin code.

Ellen Spertus
  • 6,576
  • 9
  • 50
  • 101
  • 1
    Yes of course (suspend ???) The Android OS is single threaded by default, which means operations will run by default on the UI thread except they are explicitly scheduled to run on a background thread. It's UNIX in essence so timeslice is the way it works.Guaranteed, No (and no warrantee). – Jon Goodwin Oct 03 '19 at 23:34

1 Answers1

2

All threads are equal. Any one of them can take the processor at any time. WHich one gets the processor is determined by the Linux OS scheduler underneath it, which attempts to make sure the highest priority thread gets run, while making sure that nothing gets starved if possible. That OS doesn't have the concept of a "main thread". So yes, there will be times, frequently, when both the UI thread and another thread want to run and the other thread is scheduled. You cannot count on it happening only at certain points. You have to do real multithreading and synchronization.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127