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

后端 未结 4 958
逝去的感伤
逝去的感伤 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:26

    Well, any static method in the language can be invoked on an object reference instead of the class. But that doesn't change the functionality: Thread.sleep() is a static method that puts the current thread to sleep.

    0 讨论(0)
  • 2020-12-19 23:30

    Only if called from the UI thread.

    0 讨论(0)
  • 2020-12-19 23:37

    Thread.sleep(time) will causes the thread which sent this message to sleep for the given interval of time. So if you call it in UI thread, it will affect the UI thread. As far as android is concerned it is not advised to obstruct UI thread in any way, otherwise it will result in ANR - application not responding.

    You can refer this

    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题