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:MyDaemon.java

public static void main(String args[]) throws Exception {
    MyDaemon dt = new MyDaemon();
    if (dt.isDaemon())
        System.out.println("dt is a daemon thread.");

    Thread.sleep(10000);
    System.out.println("\nMain thread ending.");
}

From source file:Main.java

public static void main(String args[]) {
    executor = Executors.newScheduledThreadPool(1);
    runnable = new Runnable() {
        @Override//from   w  w  w.j a v  a2 s  .  co m
        public void run() {
            System.out.println("Inside runnable" + i++);
        }
    };
    future = executor.scheduleWithFixedDelay(runnable, 0, 2, TimeUnit.SECONDS);
    try {
        Thread.sleep(4000); // sleeping for 2 seconds
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    future.cancel(false);
    future = executor.scheduleWithFixedDelay(runnable, 0, 10, TimeUnit.SECONDS);
}

From source file:MainClass.java

public static void main(String[] args) {
    MainClass ad7 = new MainClass();

    try {/* www  . j a va2 s  .co m*/
        Thread.sleep(5000);
    } catch (InterruptedException e) {
    }

    ad7.dumpTableInfo(ad7.getAccessibleContext());
}

From source file:SleepInterrupt.java

public static void main(String[] args) {
    SleepInterrupt si = new SleepInterrupt();
    Thread t = new Thread(si);
    t.start();//from   w  w  w  .j av a2  s. c o m

    // Be sure that the new thread gets a chance to
    // run for a while.
    try {
        Thread.sleep(2000);
    } catch (InterruptedException x) {
    }

    System.out.println("in main() - interrupting other thread");
    t.interrupt();
    System.out.println("in main() - leaving");
}

From source file:MainClass.java

public static void main(String[] args) {
    MainClass ad8 = new MainClass();

    try {//from   w w  w . jav a  2 s  .co m
        Thread.sleep(5000);
    } catch (InterruptedException e) {
    }

    ad8.dumpTextInfo(ad8.getAccessibleContext());
}

From source file:clicker.java

public static void main(String args[]) {
    Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
    clicker hi = new clicker(Thread.NORM_PRIORITY + 2);
    clicker lo = new clicker(Thread.NORM_PRIORITY - 2);

    lo.start();/*from w  w  w.j a  v a 2s .c om*/
    hi.start();
    try {
        Thread.sleep(10000);
    } catch (InterruptedException e) {
        System.out.println("Main thread interrupted.");
    }

    lo.stop();
    hi.stop();

    // Wait for child threads to terminate.
    try {
        hi.t.join();
        lo.t.join();
    } catch (InterruptedException e) {
        System.out.println("InterruptedException caught");
    }

    System.out.println("Low-priority thread: " + lo.click);
    System.out.println("High-priority thread: " + hi.click);
}

From source file:MyThread.java

public static void main(String args[]) throws Exception {
    Thread thrd = new Thread(new MyThread(), "MyThread #1");
    Thread thrd2 = new Thread(new MyThread(), "MyThread #2");
    thrd.start();/*from w  ww .ja  v  a 2 s. c o  m*/
    Thread.sleep(100);
    thrd.interrupt();
    thrd2.start();
    Thread.sleep(400);
    thrd2.interrupt();
}

From source file:DaemonThread.java

public static void main(String[] args) {
    System.out.println("entering main()");

    Thread t = new Thread(new DaemonThread());
    t.setDaemon(true);/*from   w w w.j a va2  s  . c o m*/
    t.start();

    try {
        Thread.sleep(3000);
    } catch (InterruptedException x) {
    }

    System.out.println("leaving main()");
}

From source file:TestSynchronized.java

public static void main(String[] args) throws Exception {
    final TestSynchronized tus = new TestSynchronized();

    Runnable runA = new Runnable() {
        public void run() {
            tus.performATask(1);// www  . ja va2  s. c o  m
        }
    };

    Thread ta = new Thread(runA, "threadA");
    ta.start();

    Thread.sleep(2000);

    Runnable runB = new Runnable() {
        public void run() {
            tus.performATask(2);
        }
    };

    Thread tb = new Thread(runB, "threadB");
    tb.start();
}

From source file:NewThread.java

public static void main(String args[]) {
    new NewThread(); // create a new thread

    try {//from   w  ww  . ja v a  2 s.c  o  m
        for (int i = 5; i > 0; i--) {
            System.out.println("Main Thread: " + i);
            Thread.sleep(1000);
        }
    } catch (InterruptedException e) {
        System.out.println("Main thread interrupted.");
    }
    System.out.println("Main thread exiting.");
}