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:

  1. with exactly 1-second pause between lines?
  2. 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

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -