Reputation:
Will this make the current UI thread to sleep?
try
{
Thread.sleep(20);
onProgressUpdate(i);
} catch (InterruptedException e) {
e.printStackTrace();
}
Upvotes: 4
Views: 8042
Reputation: 133560
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
Upvotes: 2
Reputation: 16739
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
Upvotes: 1
Reputation: 3607
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.
Upvotes: 0