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

后端 未结 2 1772
别跟我提以往
别跟我提以往 2020-12-31 16:24

In order to synchronize/queue access to a shared resource, I am about to use a Semaphore, aided by a wait loop.

In order not to run into CPU pegging, I would like t

2条回答
  •  礼貌的吻别
    2020-12-31 16:53

    The truth is:

    Thread.sleep(n) could be interrupted within a call like AsyncTask by using asyncTask.cancel(true)

    SystemClock.sleep(n) seems to ignore any interrupted command, thus it could be a risk of memory leak when you use it similar like here: https://github.com/square/leakcanary/blob/master/leakcanary-sample/src/main/java/com/example/leakcanary/MainActivity.java

提交回复
热议问题