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

public static void main(String[] args) {
    try {/*w ww .ja va2 s. com*/
        SyncBlock sb = new SyncBlock();

        Thread t1 = launch(sb, "T1");
        Thread.sleep(500);

        Thread t2 = launch(sb, "T2");
        Thread t3 = launch(sb, "T3");

        Thread.sleep(1000);

        print("about to interrupt T2");
        t2.interrupt();
        print("just interrupted T2");

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

From source file:TwoObjects.java

public static void main(String[] args) {
    final TwoObjects obj1 = new TwoObjects("obj1");
    final TwoObjects obj2 = new TwoObjects("obj2");

    Runnable runA = new Runnable() {
        public void run() {
            obj1.synchronizedMethod(3);/*  w  ww . ja va 2s. co m*/
        }
    };

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

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

    Runnable runB = new Runnable() {
        public void run() {
            obj2.synchronizedMethod(7);
        }
    };

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

From source file:be.anova.course.camel.components.Main.java

public static void main(String[] args) throws Exception {
    BrokerService broker = new BrokerService();
    broker.setBrokerName("CamelComponentExercise");
    broker.setPersistent(false);/*from   w w  w .j av a  2 s. c  o m*/
    broker.setUseJmx(false);
    broker.start();

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

    Thread.sleep(60000);

    broker.stop();
}

From source file:MyThread.java

public static void main(String args[]) throws Exception {
    MyThread thrd = new MyThread();
    thrd.setName("MyThread #1");
    System.out.println(thrd.getName() + " Alive:" + thrd.isAlive() + " State:" + thrd.getState());

    thrd.start();//from ww w.  j a  va2  s  . c  o m
    Thread.sleep(50);
    System.out.println(thrd.getName() + " Alive:" + thrd.isAlive() + " State:" + thrd.getState());

    thrd.waiting = false;
    Thread.sleep(50);
    System.out.println(thrd.getName() + " Alive:" + thrd.isAlive() + " State:" + thrd.getState());

    thrd.startWork();
    Thread.sleep(50);
    System.out.println(thrd.getName() + " Alive:" + thrd.isAlive() + " State:" + thrd.getState());

    if (thrd.isAlive())
        System.out.println("alive");
    System.out.println(thrd.getName() + " Alive:" + thrd.isAlive() + " State:" + thrd.getState());
}

From source file:MyThread.java

public static void main(String args[]) throws Exception {
    ThreadGroup tg = new ThreadGroup("My Group");

    MyThread thrd = new MyThread(tg, "MyThread #1");
    MyThread thrd2 = new MyThread(tg, "MyThread #2");
    MyThread thrd3 = new MyThread(tg, "MyThread #3");

    thrd.start();/*from w ww  .j  av  a2  s .com*/
    thrd2.start();
    thrd3.start();

    Thread.sleep(1000);

    System.out.println(tg.activeCount() + " threads in thread group.");

    Thread thrds[] = new Thread[tg.activeCount()];
    tg.enumerate(thrds);
    for (Thread t : thrds)
        System.out.println(t.getName());

    thrd.myStop();

    Thread.sleep(1000);

    System.out.println(tg.activeCount() + " threads in tg.");
    tg.interrupt();
}

From source file:com.apress.prospringintegration.jmx.JmxOperationInvoking.java

public static void main(String[] args) {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("jmx/operation-invoking.xml");

    MessageChannel add = context.getBean("operation", MessageChannel.class);
    add.send(MessageBuilder.withPayload("Hello").build());

    try {//from   w ww .  j a  va 2s .c o  m
        Thread.sleep(180000);
    } catch (InterruptedException e) {
        //do nothing
    }
    context.stop();
}

From source file:MyThread.java

public static void main(String args[]) throws Exception {
    MyThread mt = new MyThread("MyThread");
    Thread.sleep(100);
    mt.suspend();/*w ww .ja va  2s  .  c o  m*/
    Thread.sleep(100);

    mt.resume();
    Thread.sleep(100);

    mt.suspend();
    Thread.sleep(100);

    mt.resume();
    Thread.sleep(100);

    mt.stop();
}

From source file:com.dianping.dpsf.other.echo.SimpleEchoClient.java

/**
 * @param args/*from   ww w. j a  va2 s  .co  m*/
 * @throws Exception 
 */
public static void main(String[] args) throws Exception {

    ConfigCache.getInstance("192.168.7.41:2182");
    ApplicationContext ctx = new ClassPathXmlApplicationContext("echo-client.xml");
    IEcho s = (IEcho) ctx.getBean("echo");

    while (true) {
        try {
            System.out.println(s.echo("aa"));
            Thread.sleep(500);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

From source file:demo.vmware.app.WanGatewayWritebehind.java

public static void main(String[] args) throws Exception {
    String resource[] = { "spring-cache-gateway-writebehind.xml" };
    ClassPathXmlApplicationContext mainContext = new ClassPathXmlApplicationContext(resource, false);
    mainContext.setValidating(true);//ww w . j  ava  2  s .c om
    mainContext.refresh();

    Thread.sleep(Long.MAX_VALUE);
}

From source file:lohbihler.process.Test.java

public static void main(final String[] args) throws Exception {
    final ExecutorService executorService = Executors.newCachedThreadPool();

    final Process process = new ProcessBuilder("cmd").start();
    final InputReader input = new InputReader(process.getInputStream());
    final InputReader error = new InputReader(process.getErrorStream());

    executorService.execute(input);/*from   www.j  a  v  a 2  s .co  m*/
    executorService.execute(error);
    final OutputStreamWriter out = new OutputStreamWriter(process.getOutputStream());

    Thread.sleep(1000);
    System.out.println("Input: " + input.getInput());
    System.out.println("Error: " + error.getInput());
    System.out.println("Alive: " + process.isAlive());
    System.out.println();

    out.append("PING 1.1.1.1 -n 1 -w 5000\r\n");
    out.flush();

    for (int i = 0; i < 7; i++) {
        Thread.sleep(1000);
        System.out.println("Input: " + input.getInput());
        System.out.println("Error: " + error.getInput());
        System.out.println("Alive: " + process.isAlive());
        System.out.println();
    }

    out.append("PING 1.1.1.1 -n 1 -w 2000\r\n");
    out.flush();

    for (int i = 0; i < 4; i++) {
        Thread.sleep(1000);
        System.out.println("Input: " + input.getInput());
        System.out.println("Error: " + error.getInput());
        System.out.println("Alive: " + process.isAlive());
        System.out.println();
    }

    process.destroy();

    executorService.shutdown();
}