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

public static void main(String args[]) throws Exception {
    MyThread thrd = new MyThread();
    thrd.setName("MyThread #1");
    showThreadStatus(thrd);/*from w  w  w  .jav  a2s .c om*/

    thrd.start();
    Thread.sleep(50);
    showThreadStatus(thrd);

    thrd.waiting = false;
    Thread.sleep(50);
    showThreadStatus(thrd);

    thrd.notice();
    Thread.sleep(50);
    showThreadStatus(thrd);
    while (thrd.isAlive())
        System.out.println("alive");
    showThreadStatus(thrd);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel p = new JPanel();
    p.setOpaque(true);/*  w ww.  j a  v  a2 s .c  o  m*/
    p.setLayout(new FlowLayout());
    p.add(good);

    f.add(p, BorderLayout.CENTER);
    f.add(resultLabel, BorderLayout.SOUTH);
    good.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ev) {
            resultLabel.setText("Working . . .");
            good.setEnabled(false);
            Thread worker = new Thread() {
                public void run() {
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException ex) {
                    }
                    SwingUtilities.invokeLater(new Runnable() {
                        public void run() {
                            resultLabel.setText("Ready");
                            good.setEnabled(true);
                        }
                    });
                }
            };
            worker.start(); // So we don't hold up the dispatch thread.
        }
    });
    f.setSize(300, 100);
    f.setVisible(true);
}

From source file:org.camelcookbook.structuringroutes.simplespring.SpringCamelApplication.java

public static void main(String[] args) throws InterruptedException {
    ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(
            "META-INF/spring/simplespring-context.xml");
    applicationContext.start();//from w w w . j a va2s  .c o m

    // let the Camel runtime do its job for 5 seconds
    Thread.sleep(5000);

    // shutdown
    applicationContext.stop();
}

From source file:WordLengthCallable.java

public static void main(String[] args) throws Exception {
    int THREAD_COUNT = 4;
    ExecutorService execService = Executors.newFixedThreadPool(THREAD_COUNT);
    CompletionService<Integer> completionService = new ExecutorCompletionService<>(execService);

    for (int i = 0; i < THREAD_COUNT; i++) {
        completionService.submit(new WordLengthCallable());
    }//w w w . j a  v  a2  s  .  co  m
    execService.shutdown();
    while (!execService.isTerminated()) {
        int result = completionService.take().get().intValue();
        System.out.println("Result is: " + result);
    }
    Thread.sleep(1000);
    System.out.println("done!");
}

From source file:org.camelcookbook.structuringroutes.simplespring.SpringJavaDslApplication.java

public static void main(String[] args) throws InterruptedException {
    ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(
            "META-INF/spring/simplespring-java-context.xml");
    applicationContext.start();/*from  w ww  . j  a v a2s  . co m*/

    // let the Camel runtime do its job for 5 seconds
    Thread.sleep(5000);

    // shutdown
    applicationContext.stop();
}

From source file:BasicTimer.java

public static void main(String[] args) throws Exception {
    int type = TYPE_REMAINING;
    BasicTimer timer = new BasicTimer(type);
    timer.setMax(10000);//  ww  w.  j a v  a  2s.c  om
    GregorianCalendar calendar = new GregorianCalendar();
    while (true) {
        Thread.sleep(1000);
        if (type == TYPE_CLOCK) {
            calendar.setTimeInMillis(timer.getTime());
            System.out.println("Clock: " + calendar);
        } else if (type == TYPE_ELAPSED) {
            System.out.println("Elapsed: " + (timer.getTime() / 1000) + " seconds");
        } else if (type == TYPE_REMAINING) {
            System.out.println("Remaining: " + (timer.getTime() / 1000) + " seconds");
        }
    }
}

From source file:net.vnt.ussdapp.USSDApp.java

public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "/context.xml" });
    ContextProperties properties = (ContextProperties) Context.getInstance().getBean("ContextProperties");
    try {//from   ww  w.  ja  v  a  2  s . co m
        if (USSD.initialize() == 0) {
            System.out.println("Error initializing");
            return;
        }
        if (USSD.connect(properties.getProperty("ip.ussdgw"), properties.getInt("port.ussdgw")) == 0) {
            System.out.println("Error connecting");
            return;
        }
        while (true) {
            Thread.sleep(1000);
        }
    } catch (InterruptedException ex) {
        ex.printStackTrace();
    }
}

From source file:SelfRunThreadTemplate.java

public static void main(String[] args) {
    SelfRunThreadTemplate sr = new SelfRunThreadTemplate();
    try {/*  w  w  w.  j  ava2  s.  co  m*/
        Thread.sleep(3000);
    } catch (InterruptedException x) {
    }
    sr.stopRequest();
}

From source file:NewThread.java

public static void main(String args[]) {
    NewThread ob1 = new NewThread("One");
    NewThread ob2 = new NewThread("Two");

    try {//  w w w  .  ja  va2  s  . c om
        Thread.sleep(1000);
        ob1.mysuspend();
        System.out.println("Suspending thread One");
        Thread.sleep(1000);
        ob1.myresume();
        System.out.println("Resuming thread One");
        ob2.mysuspend();
        System.out.println("Suspending thread Two");
        Thread.sleep(1000);
        ob2.myresume();
        System.out.println("Resuming thread Two");
    } catch (InterruptedException e) {
        System.out.println("Main thread Interrupted");
    }

    try {
        System.out.println("Waiting for threads to finish.");
        ob1.t.join();
        ob2.t.join();
    } catch (InterruptedException e) {
        System.out.println("Main thread Interrupted");
    }

    System.out.println("Main thread exiting.");
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Path path = Paths.get("test.txt");

    try (AsynchronousFileChannel afc = AsynchronousFileChannel.open(path, StandardOpenOption.WRITE,
            StandardOpenOption.CREATE)) {
        ByteBuffer dataBuffer = getDataBuffer();
        Future<Integer> result = afc.write(dataBuffer, 0);
        while (!result.isDone()) {
            System.out.println("Sleeping for 2  seconds...");
            Thread.sleep(2000);
        }/*w  ww  .  j  a  va 2 s .  c o  m*/
        int writtenBytes = result.get();
        System.out.format("%s bytes written  to  %s%n", writtenBytes, path.toAbsolutePath());

    } catch (IOException e) {
        e.printStackTrace();
    }
}