If only one thread (main) and sleep (1000) are called, will the thread sleep for exactly 1 second, or at least 1 second?

In the code below:

 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);
           }
      }
 }

      

Each line in the rows of the array will be output:

  • With exactly 1 second pause between lines?
  • With at least 1 second pause between lines?
+3


source to share


6 answers


Pause approximately 1 second. A thread can be woken up in advance and you get InterruptedException

, or a thread can sleep for 1000ms and then not start immediately, so it will be 1000ms + microseconds (or more if there are higher CPU hogging thread priorities).



You also call it wrong. It Thread.sleep(1000);

's like a static method, it always acts on the current thread and you can't deal with it.

+7


source


Thus, it will sleep for exactly 1 second as much as possible . The thread.sleep method is not perfect. See this and other related questions:



fooobar.com/questions/309460 / ...

+1


source


Some points about Thread.sleep
1. it is always the current thread that sleeps
2. the thread may not sleep for the required amount of time (or even at all); sleep duration will be subject to some system-specific granularity, typically 1ms;
3. while sleeping, the thread still owns the syncs it acquired;
4. Sleep can be interrupted (sometimes useful to implement the undo function);
5. calling sleep () with certain values ​​can have some subtle global effects for the OS.

So at the end you can each line output contain at least 1 second of pause between lines.
And you call it wrong. This is a static method .. :)

+1


source


Calling the Thread.sleep (1000) method, put the currently executing thread in a wait state for the specified time. According to your program, it seems to be only one threading program, so when the calling thread is in a waiting state, no other thread is in a starting state, so after 1000ms your thread will be able to execute after almost 1000ms, but not sure about other application in which none of the threads will run.

+1


source


It will sleep for at least 1 second unless interrupted by some other thread. If any other thread interrupts it, will be selected InterruptedException

.

Ideally, it will sleep for 1 second, once that time has elapsed, it will wait for it to return to working state again.

0


source


Read the documentation. (Why didn't anyone say this?) The Javadoc for Thread.sleep () says,

Causes the currently executing thread to sleep (temporarily stop executing) in the specified number of milliseconds, taking into account the precision and accuracy of system timers and schedulers.

It's pretty vague, but what guarantees you get. The exact behavior will depend on what operating system you are using, and possibly what JVM you are using.

An application that requires accurate time is called a real-time application, and there are special real-time operating systems (RTOS) that will tell you how many microseconds of scheduled time will actually happen. you can even run live Java versions to run on RTOS. http://en.wikipedia.org/wiki/Real_time_Java

0


source







All Articles