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

public static void main(String[] args) {
    Timer timer = new Timer();
    GCTask task = new GCTask();
    timer.schedule(task, 5000, 5000);/*from   w w w  .j  a v a 2s.  c o m*/
    int counter = 1;
    while (true) {
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
        }
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JOptionPane pane = new JOptionPane("your message", JOptionPane.ERROR_MESSAGE, JOptionPane.OK_OPTION);
    JDialog d = pane.createDialog(null, "title");
    d.pack();//from w ww.  j  av  a 2 s  .  c  om
    d.setModal(false);
    d.setVisible(true);
    while (pane.getValue() == JOptionPane.UNINITIALIZED_VALUE) {
        try {
            Thread.sleep(100);
        } catch (InterruptedException ie) {
        }
    }
    System.exit(0);
}

From source file:AutoTask.java

public static void main(String args[]) {
    AutoTask myTask = new AutoTask();
    Timer bkTimer = new Timer();

    bkTimer.schedule(myTask, 2000, 2000);

    for (int i = 0; i < 5; i++) {
        try {//ww w.ja  v a  2  s  .c om
            Thread.sleep(2100);
        } catch (InterruptedException exc) {
        }
    }
    bkTimer.cancel();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Runner t = new Runner();
    Thread t1 = new Thread(t);
    for (int i = 0; i < 50; i++) {
        t.queue.add(i);//  w  ww.  j a  v  a  2 s . c  om
    }
    System.out.println(("Number of items in queue: " + t.queue.size()));
    t1.start();
    Thread.sleep(1000);
    t1.interrupt();
    t1.join();
    System.out.println(("Number of items in queue: " + t.queue.size()));
    System.out.println(("Joined t1. Finished"));
}

From source file:CounterThread.java

public static void main(String[] args) {
    CounterThread thread = new CounterThread();
    thread.start();/*from  w  ww .j  a  v a  2 s . c  o  m*/
    try {
        Thread.sleep(10000);
    } catch (InterruptedException e) {
    }
    thread.stopped = true;
    System.out.println("exit");
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    User a = new User("A", "B");
    System.out.println("logon a = " + a);
    ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream("User.out"));
    o.writeObject(a);/*w ww.ja  v  a2  s . co m*/
    o.close();

    Thread.sleep(1000); // Delay for 1 second

    ObjectInputStream in = new ObjectInputStream(new FileInputStream("User.out"));
    System.out.println("Recovering object at " + new Date());
    a = (User) in.readObject();
    System.out.println("logon a = " + a);
}

From source file:FileLocking.java

public static void main(String[] args) throws Exception {
    FileOutputStream fos = new FileOutputStream("file.txt");
    FileLock fl = fos.getChannel().tryLock();
    if (fl != null) {
        System.out.println("Locked File");
        Thread.sleep(100);
        fl.release();/*w  w  w .  j  ava 2  s . co  m*/
        System.out.println("Released Lock");
    }
    fos.close();
}

From source file:Main.java

public static void main(String[] args) {
    try {//from www  .jav a 2s  .com
        // create a new process
        Process p = Runtime.getRuntime().exec("notepad.exe");

        // get the input stream of the process and print it
        InputStream in = p.getInputStream();
        for (int i = 0; i < in.available(); i++) {
            System.out.println(in.read());
        }

        // wait for 10 seconds and then destroy the process
        Thread.sleep(10000);
        p.destroy();

    } catch (Exception ex) {
        ex.printStackTrace();
    }

}

From source file:Main.java

public static void main(String[] args) {
    try {// ww w.  ja  v a  2 s. c om
        // create a new process
        Process p = Runtime.getRuntime().exec("notepad.exe");

        // get the error stream of the process and print it
        InputStream error = p.getErrorStream();
        for (int i = 0; i < error.available(); i++) {
            System.out.println(error.read());
        }

        // wait for 10 seconds and then destroy the process
        Thread.sleep(10000);
        p.destroy();

    } catch (Exception ex) {
        ex.printStackTrace();
    }

}

From source file:Test.java

public static void main(String[] args) {
    final Lock firstLock = new ReentrantLock();
    final Lock secondLock = new ReentrantLock();
    firstLock.lock();//from w w w  .  j  a v  a2 s . co m
    Thread secondThread = new Thread(new Runnable() {
        public void run() {
            secondLock.lock();
            firstLock.lock();
        }
    });
    secondThread.start();
    try {
        Thread.sleep(250);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    secondLock.lock();

    secondLock.unlock();
    firstLock.unlock();
}