3

In the 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 the array lines output:

  1. With exactly 1-second pause between lines?
  2. With at least 1-second pause between lines?
AncientSwordRage
  • 7,086
  • 19
  • 90
  • 173
paidedly
  • 1,413
  • 1
  • 13
  • 22

6 Answers6

7

Approximately 1-second pause. The thread can be woken up beforehand and you'll get an InterruptedException, or the thread can sleep for 1000ms and then not get to run immediately, so it will be 1000ms + microseconds (or more, if there are higher priority threads hogging the CPU).

You're also calling it wrong. It's Thread.sleep(1000);, as a static method it always acts on the current thread and you can't make other threads sleep with it.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
  • 1
    I think even the "at least" is not guaranteed and is system dependent. When I time a Thread.sleep using System.nanoTime on my machine (Intel core I7) it regularly sleeps for less than 1000ms, e.g. 999.75 ms. I even had 995.5 ms. – Henno Vermeulen Jun 18 '15 at 08:05
  • 1
    @HennoVermeulen Well, `nanoTime` has its own precision problems, so better not include it here. – Kayaman Jun 18 '15 at 08:06
  • Well first off I am no expert on these matters. What you're saying does surprise me because I always thought nanoTime was very accurate. I always assumed it was at least less than one millisecond off for measuring elapsed time. Anyway it still seems plausible to me that Thread.sleep could really sleep for less than 1000ms on an I7: I see the clock speed change regularly in Windows task manager and I can imagine this may influence the sleep time. Again I'm no expert so I may be wrong on this. Perhaps I should make a new question :). – Henno Vermeulen Jun 18 '15 at 08:21
  • By all means create a new question if something is unclear. Beforehand I recommend searching for questions about nanoTime, you'll note that it's not as straightforward a beast as it seems either. – Kayaman Jun 18 '15 at 08:33
  • You're absolutely right... System.nanoTime does have issues. It seems even the often heard statement "prefer System.nanoTime over System.currentTimeMillis because it is much more accurate" is not true under all circumstances. System.currentTimeMillis never reports less than 1000ms for me while (right after starting the program,) nanoTime reports as low as 985.4 ms. – Henno Vermeulen Jun 18 '15 at 08:34
  • I did not get it clearly. This answer proclamates "at least 1-second pause"? – oopexpert Dec 20 '15 at 17:28
1

So it will sleep for exactly 1 second to the best of it's knowledge. The thread.sleep method is not perfect. See this and other related questions:

https://stackoverflow.com/a/18737109/4615177

Community
  • 1
  • 1
George
  • 903
  • 4
  • 19
1

Some points about Thread.sleep
1. it is always the current thread that is put to sleep
2. the thread might not sleep for the required time (or even at all); the sleep duration will be subject to some system-specific granularity, typically 1ms;
3. while sleeping, the thread still owns synchronization locks it has acquired;
4. the sleep can be interrupted (sometimes useful for implementing a cancellation function);
5. calling sleep() with certain values can have some subtle, global effects on the OS

So at the end you can each String output will be with at least 1-second pause between lines.
And you calling it wrong. It is a static method..:)

Viraj Nalawade
  • 3,137
  • 3
  • 28
  • 44
1

Calling Thread.sleep(1000) method, put the current executing thread in waiting state for the specified time. As per your program, it seems only a single threaded program hence,while The calling thread is in waiting state, no other thread is in running state, so after 1000 ms your thread will get chance to execute almost after 1000ms but not sure for other application where no of threads are going to execute.

0

It'll sleep for at least 1 second if not interrupted by some other thread. If some other thread interrupts it, InterruptedException will be thrown.

Ideally, it'll sleep for 1 second, once that time has elapsed, it will wait for it's turn to get into running state again.

justAbit
  • 4,226
  • 2
  • 19
  • 34
0

Read the documentation. (Why did no one else say that?) The javadoc for Thread.sleep() says,

Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds, subject to the precision and accuracy of system timers and schedulers.

That's pretty vague, but that's all the guarantee you will get. The exact behavior will depend on what operating system you are running and probably on what JVM you are running.

An application that requires precise timing is called a real-time application, and there are special real-time operating systems (RTOS) that will tell you within how many microseconds of its scheduled time an event actually will occur. you can even get real-time versions of Java to run on your RTOS. http://en.wikipedia.org/wiki/Real_time_Java

Solomon Slow
  • 25,130
  • 5
  • 37
  • 57