java - If there's only one thread running(main) and sleep(1000) is invoked, will the thread sleep for exactly 1 second or atleast 1 second? -
in below code:
class test { public static void main(string [] args) { printall(args); } public static void printall(string[] lines) { for(int i=0;i<lines.length;i++){ system.out.println(lines[i]); thread.currentthread().sleep(1000); } } }
will each string in array lines output:
- with exactly 1-second pause between lines?
- with at least 1-second pause between lines?
approximately 1-second pause. thread can woken beforehand , you'll interruptedexception
, or thread can sleep 1000ms , not run immediately, 1000ms + microseconds (or more, if there higher priority threads hogging cpu).
you're calling wrong. it's thread.sleep(1000);
, static method acts on current thread , can't make other threads sleep it.
Comments
Post a Comment