Example usage for java.lang Thread sleep

List of usage examples for java.lang Thread sleep

Introduction

In this page you can find the example usage for java.lang Thread sleep.

Prototype

public static native void sleep(long millis) throws InterruptedException;

Source Link

Document

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.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    long start = System.currentTimeMillis();

    Thread.sleep(2000);

    // Get elapsed time in milliseconds
    long elapsedTimeMillis = System.currentTimeMillis() - start;

    // Get elapsed time in minutes
    float elapsedTimeMin = elapsedTimeMillis / (60 * 1000F);
    System.out.println(elapsedTimeMin);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    long start = System.currentTimeMillis();

    Thread.sleep(2000);

    // Get elapsed time in milliseconds
    long elapsedTimeMillis = System.currentTimeMillis() - start;

    // Get elapsed time in hours
    float elapsedTimeHour = elapsedTimeMillis / (60 * 60 * 1000F);
    System.out.println(elapsedTimeHour);
}

From source file:Main.java

public static void main(String[] args) {
    try {//from  www  . ja  va2s  . c  om
        System.out.println("sleep for 5  seconds.");
        Thread.sleep(5000);
        // The "main" thread will sleep
        System.out.println("woke up.");
    } catch (InterruptedException e) {
        System.out.println("interrupted.");
    }
    System.out.println("done.");
}

From source file:Main.java

public static void main(String[] args) throws InterruptedException {
    Thread t = new Thread(Main::run);
    t.start();//from   w w  w  .  j a va  2  s . c  o m
    Thread.sleep(5000);
    t.interrupt();
}

From source file:Main.java

public static void main(String[] args) {
    for (int i = 0; i < 10; i++) {
        System.out.println("i = " + i);

        try {/*from ww  w .  j  av a  2s.c o m*/
            Thread.sleep(1000);

        } catch (InterruptedException ie) {
            ie.printStackTrace();
        }
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    long startTime = System.currentTimeMillis();

    for (int i = 0; i < 10; i++) {
        Thread.sleep(60);
    }// w  w w .j a  v  a2 s . c o  m
    long endTime = System.currentTimeMillis();
    float seconds = (endTime - startTime) / 1000F;
    System.out.println(Float.toString(seconds) + " seconds.");
}

From source file:PendingInterrupt.java

public static void main(String[] args) {
    Thread.currentThread().interrupt();

    long startTime = System.currentTimeMillis();
    try {/*from   w  w w  .  ja v a 2s  . c o m*/
        Thread.sleep(2000);
        System.out.println("NOT interrupted");
    } catch (InterruptedException x) {
        System.out.println("Interrupted");
    }

    System.out.println("elapsedTime=" + (System.currentTimeMillis() - startTime));
}

From source file:MainClass.java

public static void main(String[] args) {
    Timer timer = new Timer();

    timer.schedule(new DisplayQuestionTask(), 0, 10 * 1000);

    try {//from w w  w .  ja va 2  s  . c  om
        Thread.sleep(10000);
    } catch (InterruptedException e) {
    }

    timer.cancel();

}

From source file:MainClass.java

License:asdf

public static void main(String[] args) {
    Timer timer = new Timer(1000, new MyTimerActionListener());

    timer.start();// ww  w.j  a va2  s . c om
    try {
        Thread.sleep(10000);
    } catch (InterruptedException e) {
    }
    timer.stop();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Robot r = new Robot();
    r.mouseMove(35, 35);//w  w w. j  a v  a2  s . co m
    r.mousePress(InputEvent.BUTTON1_MASK);
    r.mouseRelease(InputEvent.BUTTON1_MASK);
    Thread.sleep(50);
    r.mousePress(InputEvent.BUTTON1_MASK);
    r.mouseRelease(InputEvent.BUTTON1_MASK);
}