简体   繁体   中英

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

Will this make the current UI thread to sleep?

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

仅当从UI线程调用时

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM