80,471
社区成员




/**
* Waits a given number of milliseconds (of uptimeMillis) before returning.
* Similar to {@link java.lang.Thread#sleep(long)}, but does not throw
* {@link InterruptedException}; {@link Thread#interrupt()} events are
* deferred until the next interruptible operation. Does not return until
* at least the specified number of milliseconds has elapsed.
*
* @param ms to sleep before returning, in milliseconds of uptime.
*/
public static void sleep(long ms)
{
long start = uptimeMillis();
long duration = ms;
boolean interrupted = false;
do {
try {
Thread.sleep(duration);
}
catch (InterruptedException e) {
interrupted = true;
}
duration = start + ms - uptimeMillis();
} while (duration > 0);
if (interrupted) {
// Important: we don't want to quietly eat an interrupt() event,
// so we make sure to re-interrupt the thread so that the next
// call to Thread.sleep() or Object.wait() will be interrupted.
Thread.currentThread().interrupt();
}
}
08-16 12:55:34.023: DEBUG/Hello(22257): time:0
08-16 12:55:34.023: DEBUG/Hello(22257): 10
08-16 12:55:34.023: DEBUG/Hello(22257): 20
08-16 12:55:34.023: DEBUG/Hello(22257): 30
08-16 12:55:34.023: DEBUG/Hello(22257): 41
08-16 12:55:34.023: DEBUG/Hello(22257): 51
08-16 12:55:34.023: DEBUG/Hello(22257): 61
08-16 12:55:34.023: DEBUG/Hello(22257): 71
08-16 12:55:34.023: DEBUG/Hello(22257): 81
08-16 12:55:34.023: DEBUG/Hello(22257): 92
08-16 12:55:34.023: DEBUG/Hello(22257): 102
08-16 12:55:34.023: DEBUG/Hello(22257): 113
08-16 12:55:34.023: DEBUG/Hello(22257): 123
08-16 12:55:34.023: DEBUG/Hello(22257): 133
08-16 12:55:34.023: DEBUG/Hello(22257): 143
08-16 12:55:34.023: DEBUG/Hello(22257): 154
08-16 12:55:34.023: DEBUG/Hello(22257): 164
08-16 12:55:34.023: DEBUG/Hello(22257): 174
08-16 12:55:34.023: DEBUG/Hello(22257): 184
08-16 12:55:34.023: DEBUG/Hello(22257): 194
TextView tv = new TextView(this);
long start=System.nanoTime();
String s = "time:";
for (int i=0; i<20; i++) {
long now=System.nanoTime();
SystemClock.sleep(10);
s += String.valueOf((now-start)/1000000) + " \n";
}
tv.setText( s );
Log.d("Hello", s);