android - SystemClock.sleep() vs. Thread.sleep() while waiting for a semaphore loop -


in order synchronize/queue access shared resource, use semaphore, aided wait loop.

in order not run cpu pegging, sleep() little bit inside while loop.

i searched http://developer.android.com reference , found 2 such sleep() functions , confused 1 fits scenario:

  1. thread.sleep()
  2. systemclock.sleep()

which 1 better suits case described , why?

first of all, need wait loop? can typically solve problems using proper notifications, i.e. having object, calling wait() , notify() on or other means (like blocking queue, or semaphore.acquire() in case).

that said, if want polling loop (which shouldn't unless have to), i'd stick thread.sleep(). there's not of difference, documentation says, except have option interrupt thread.sleep(). don't rid option so.

note in case of thread.sleep(), you're going have catch exception - if you're extremely lazy, you'll stick systemclock.sleep().


Comments