Does Thread.sleep() makes the UI Thread to sleep?

后端 未结 4 957
逝去的感伤
逝去的感伤 2020-12-19 22:43

Will this make the current UI thread to sleep?

try 
{
   Thread.sleep(20);
   onProgressUpdate(i);
} catch (InterruptedException e) {
      e.printStackTrace         


        
4条回答
  •  悲&欢浪女
    2020-12-19 23:44

    If you are calling sleep on the ui thread it blocks the ui thread. Do not call sleep on the ui thread. You should not block the ui thread.

    http://developer.android.com/reference/java/lang/Thread.html

    http://docs.oracle.com/javase/tutorial/essential/concurrency/sleep.html

    You will get ANR

    If you implement Thread or HandlerThread, be sure that your UI thread does not block while waiting for the worker thread to complete—do not call Thread.wait() or Thread.sleep().

    http://developer.android.com/training/articles/perf-anr.html

    Also check the topic under How to Avoid ANRs

提交回复
热议问题