Java Thread.sleep(long millis, int nanos)

Syntax

Thread.sleep(long millis, int nanos) has the following syntax.

public static void sleep(long millis,  int nanos)   throws InterruptedException

Example

In the following code shows how to use Thread.sleep(long millis, int nanos) method.


/*from   www  .j  a v a2s  .  c  o  m*/
class ThreadDemo implements Runnable {

  public void run() {
    for (int i = 1; i < 13; i++) {

      System.out.println(Thread.currentThread().getName() + "  " + i);
      try {
        // thread to sleep for 1000 milliseconds plus 500 nanoseconds
        Thread.sleep(1000, 500);
      } catch (Exception e) {
        System.out.println(e);
      }
    }
  }
}

public class Main {

  public static void main(String[] args) throws Exception {
    Thread t = new Thread(new ThreadDemo());
    t.start();

  }
}




















Home »
  Java Tutorial »
    java.lang »




Boolean
Byte
Character
Class
Double
Enum
Float
Integer
Long
Math
Number
Object
Package
Process
ProcessBuilder
Runnable
Runtime
SecurityManager
Short
StackTraceElement
StrictMath
String
StringBuffer
StringBuilder
System
Thread
ThreadGroup
ThreadLocal
Throwable