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

public static void main(String args[]) {
    new NewThread();
    try {// w ww . j  ava 2  s  .com
        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.");
}

From source file:MainClass.java

public static void main(String[] args) {
    MainClass ad9 = new MainClass();
    try {//  w  w  w  .  j  a va 2  s .  co m
        Thread.sleep(5000);
    } catch (InterruptedException e) {
    }

    ad9.dumpValueInfo(ad9.getAccessibleContext());
}

From source file:MyThread.java

public static void main(String args[]) {
    System.out.println("Main thread starting.");
    MyThread mt = new MyThread();
    mt.start();/*from w  w  w  .  ja v a 2 s  .c om*/
    do {
        System.out.println("In main thread.");
        try {
            Thread.sleep(250);
        } catch (InterruptedException exc) {
            System.out.println("Main thread interrupted.");
        }
    } while (mt.count != 5);
    System.out.println("Main thread ending.");
}

From source file:MyThread.java

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

    ThreadGroup group = new ThreadGroup("new Group");

    MyThread t1 = new MyThread(group, "Thread1");
    MyThread t2 = new MyThread(group, "Thread2");

    t1.start();/*from  w  ww . ja v a  2  s .c  om*/
    t2.start();

    Thread.sleep(1000);

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

    Thread th[] = new Thread[group.activeCount()];
    group.enumerate(th);
    for (Thread t : th) {
        System.out.println(t.getName());
    }
    Thread.sleep(1000);

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

    group.interrupt();
}

From source file:MainClass.java

public static void main(String[] argv) throws Exception {
    int port = 1234; // default

    ByteBuffer buffer = ByteBuffer.wrap(GREETING.getBytes());
    ServerSocketChannel ssc = ServerSocketChannel.open();

    ssc.socket().bind(new InetSocketAddress(port));
    ssc.configureBlocking(false);//  ww w  .j a  v  a 2s .c om

    while (true) {
        System.out.println("Waiting for connections");

        SocketChannel sc = ssc.accept();

        if (sc == null) {
            Thread.sleep(2000);
        } else {
            System.out.println("Incoming connection from: " + sc.socket().getRemoteSocketAddress());

            buffer.rewind();
            sc.write(buffer);
            sc.close();
        }
    }
}

From source file:MyThread.java

public static void main(String args[]) {
    Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
    MyThread hi = new MyThread(Thread.NORM_PRIORITY + 2);
    MyThread lo = new MyThread(Thread.NORM_PRIORITY - 2);
    lo.start();//from  ww  w .  ja  v a2  s . c o m
    hi.start();

    try {
        Thread.sleep(10000);
    } catch (Exception e) {
    }

    lo.stop();
    hi.stop();
    System.out.println(lo.click + " vs. " + hi.click);
}

From source file:NewThread.java

public static void main(String args[]) {
    new NewThread("One"); // start threads
    new NewThread("Two");
    new NewThread("Three");

    try {/*w w  w. ja  va 2  s  .  c  om*/
        Thread.sleep(10000);
    } catch (InterruptedException e) {
        System.out.println("Main thread Interrupted");
    }

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

From source file:StaticSync.java

public static void main(String[] args) {
    try {/*from ww  w . j  a v a2s . c o  m*/
        Runnable r = new Runnable() {
            public void run() {
                print("getNextSerialNum()=" + getNextSerialNum());
            }
        };

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

        Thread.sleep(1500);

        Thread threadB = new Thread(r, "threadB");
        threadB.start();

        Thread.sleep(500);

        Thread threadC = new Thread(r, "threadC");
        threadC.start();

        Thread.sleep(2500);

        Thread threadD = new Thread(r, "threadD");
        threadD.start();
    } catch (InterruptedException x) {
        // ignore
    }
}

From source file:ThreadIDMain.java

public static void main(String[] args) {
    ThreadID tid = new ThreadID();
    ThreadIDMain shared = new ThreadIDMain(tid);

    try {/*  w  w  w . j a  v  a2 s. c  om*/
        Thread threadA = new Thread(shared, "threadA");
        threadA.start();

        Thread.sleep(500);

        Thread threadB = new Thread(shared, "threadB");
        threadB.start();

        Thread.sleep(500);

        Thread threadC = new Thread(shared, "threadC");
        threadC.start();
    } catch (InterruptedException x) {
    }
}

From source file:MyThread.java

public static void main(String args[]) {
    System.out.println("Main thread starting.");
    MyThread mt = new MyThread();
    Thread newThrd = new Thread(mt);
    newThrd.start();//from w  w w.  j av  a2s.com
    do {
        System.out.println("In main thread.");
        try {
            Thread.sleep(250);
        } catch (InterruptedException exc) {
            System.out.println("Main thread interrupted.");
        }
    } while (mt.count != 5);

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