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

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    ProgressBar bar = new ProgressBar(shell, SWT.SMOOTH);
    bar.setBounds(10, 10, 200, 32);/*from   w  w  w  . j ava2  s.  c  o m*/
    shell.open();
    for (int i = 0; i <= bar.getMaximum(); i++) {
        try {
            Thread.sleep(100);
        } catch (Throwable th) {
        }
        bar.setSelection(i);
    }
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:AnotherDeadLock.java

public static void main(String[] args) {
    final Object resource1 = "resource1";
    final Object resource2 = "resource2";
    // t1 tries to lock resource1 then resource2
    Thread t1 = new Thread() {
        public void run() {
            // Lock resource 1
            synchronized (resource1) {
                System.out.println("Thread 1: locked resource 1");

                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                }/*from ww w . j  a  va2  s  . c om*/

                synchronized (resource2) {
                    System.out.println("Thread 1: locked resource 2");
                }
            }
        }
    };

    // t2 tries to lock resource2 then resource1
    Thread t2 = new Thread() {
        public void run() {
            synchronized (resource2) {
                System.out.println("Thread 2: locked resource 2");

                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                }

                synchronized (resource1) {
                    System.out.println("Thread 2: locked resource 1");
                }
            }
        }
    };

    // If all goes as planned, deadlock will occur,
    // and the program will never exit.
    t1.start();
    t2.start();
}

From source file:Main.java

public static void main(String[] args) throws InterruptedException {
    JFrame f = new JFrame();

    DefaultTableModel m = new DefaultTableModel();
    JTable t = new JTable(m);

    f.add(new JScrollPane(t), BorderLayout.CENTER);
    f.setVisible(true);/*from  w  ww.  j a  v  a2s  . co m*/
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.pack();
    m.addColumn("A");
    m.addRow(new String[] { "A1" });

    Thread.sleep(2000);

    m.addColumn("B");
    m.addRow(new String[] { "A2", "B2" });
}

From source file:Main.java

public static void main(String[] args) {
    Thread t = Thread.currentThread();
    Main t1 = new Main(3);
    t1.createThread("Child 1");
    t1.createThread("Child 2");
    for (int i = 0; i <= 5; i++) {
        try {//  w w w . ja  v a2 s  .com
            Thread.sleep(1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("The cirrent Thread is " + t.getName() + " and thread ID is " + t.getId());
    }
}

From source file:MyTimerTask.java

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

    /*/* www.  j a  va 2 s  .  c o  m*/
     * Set an initial delay of 1 second, then repeat every half second.
     */
    myTimer.schedule(myTask, 1000, 500);

    try {
        Thread.sleep(5000);
    } catch (InterruptedException exc) {
    }

    myTimer.cancel();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Main v = new Main();
    v.start();//from   ww w .j  av  a2s  .  c o m
    Thread.sleep(3000);
    System.out.println("Going to set the stop flag to true");
    v.stopThread();
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    SplashScreen splash = SplashScreen.getSplashScreen();
    Graphics2D g = (Graphics2D) splash.createGraphics();
    Dimension dim = splash.getSize();
    for (int i = 0; i < 100; i++) {
        g.setColor(Color.RED);// w  w w.  j  a  v a2 s . co m
        g.fillRect(50, 50, dim.width - 100, dim.height - 100);
        splash.update();
        try {
            Thread.sleep(250);
        } catch (InterruptedException ignored) {
        }
    }
    JFrame frame = new JFrame("Splash Me2");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JLabel label = new JLabel("Hello, Splash", JLabel.CENTER);
    frame.add(label, BorderLayout.CENTER);
    frame.setSize(300, 95);
    frame.setVisible(true);
}

From source file:pl.edu.amu.wmi.shop.Shop.java

public static void main(String[] args) throws InterruptedException {

    ApplicationContext context = new ClassPathXmlApplicationContext("Context.xml");

    Thread.sleep(1000);

    PublicKey publicKey = ((BankPublicKeyReceiverService) context.getBean("bankPublicKeyReceiverService"))
            .getBankPublicKey();//from   w w w.ja va 2  s. c  om

}

From source file:TwoThreadSleep.java

public static void main(String[] args) {
    TwoThreadSleep tt = new TwoThreadSleep();
    tt.setName("thread");
    tt.start();/* ww w . j a va2 s  .c  om*/

    // pause for a bit
    try {
        Thread.sleep(700);
    } catch (InterruptedException x) {
        // ignore
    }
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    SplashScreen splash = SplashScreen.getSplashScreen();
    Graphics2D g = (Graphics2D) splash.createGraphics();
    System.out.println(splash.getBounds());
    Dimension dim = splash.getSize();
    for (int i = 0; i < 100; i++) {
        g.setColor(Color.RED);/* w  ww  . j  a  v  a2 s .c o m*/
        g.fillRect(50, 50, dim.width - 100, dim.height - 100);
        splash.update();
        try {
            Thread.sleep(250);
        } catch (InterruptedException ignored) {
        }
    }
    JFrame frame = new JFrame("Splash Me2");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JLabel label = new JLabel("Hello, Splash", JLabel.CENTER);
    frame.add(label, BorderLayout.CENTER);
    frame.setSize(300, 95);
    frame.setVisible(true);
}