Example usage for java.lang Object wait

List of usage examples for java.lang Object wait

Introduction

In this page you can find the example usage for java.lang Object wait.

Prototype

public final native void wait(long timeoutMillis) throws InterruptedException;

Source Link

Document

Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.

Usage

From source file:Main.java

public static void main(String str[]) {
    final Object monitor = new Object();
    new Thread() {
        public void run() {
            try {
                synchronized (monitor) {
                    System.out.println("10 seconds ...");
                    monitor.wait(10000);
                    System.out.println("Wait over");
                }/*w ww  . j av  a 2s .  com*/
            } catch (Throwable t) {
                t.printStackTrace();
            }
        }
    }.start();
}

From source file:Main.java

public static void main(String[] args) {
    int maximum = 100;
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Integer[] oneRow = { 0, 0, 0, 0 };
    String[] headers = { "A", "B", "C", "D" };
    Integer[][] data = { oneRow, oneRow, oneRow, oneRow, oneRow, };
    DefaultTableModel model = new DefaultTableModel(data, headers);
    JTable table = new JTable(model);
    table.setDefaultRenderer(Object.class, new ProgressRenderer(0, maximum));
    table.setPreferredScrollableViewportSize(table.getPreferredSize());
    frame.add(new JScrollPane(table));
    frame.pack();//ww w  .ja va2  s. c  om
    frame.setVisible(true);
    new Thread(new Runnable() {
        @Override
        public void run() {
            Object waiter = new Object();
            synchronized (waiter) {
                int rows = model.getRowCount();
                int columns = model.getColumnCount();
                Random random = new Random(System.currentTimeMillis());
                boolean done = false;
                while (!done) {
                    int row = random.nextInt(rows);
                    int column = random.nextInt(columns);
                    Integer value = (Integer) model.getValueAt(row, column);
                    value++;
                    if (value <= maximum) {
                        model.setValueAt(value, row, column);
                        try {
                            waiter.wait(15);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    done = true;
                    for (row = 0; row < rows; row++) {
                        for (column = 0; column < columns; column++) {
                            if (!model.getValueAt(row, column).equals(maximum)) {
                                done = false;
                                break;
                            }
                        }
                        if (!done) {
                            break;
                        }
                    }
                }
            }
        }
    }).start();
}

From source file:Main.java

public static void waitIgnoreException(Object monitor, long timeout) {
    try {/*from   ww  w. java  2s. com*/
        monitor.wait(timeout);
    } catch (InterruptedException ignored) {
    }
}

From source file:Main.java

public static void waitNoShit(Object o, int timeout) {
    try {/*  w  w  w.jav a  2 s .  c om*/
        o.wait(timeout);
    } catch (InterruptedException e) {
    }
}

From source file:Main.java

public static void wait(Object o, long time) {
    try {/*  w ww .  j  a  va  2s. c  om*/
        o.wait(time);
    } catch (InterruptedException e) {
    }
}

From source file:Main.java

public static void wait(Object lock, long timeout) {
    try {//w ww. j  av a  2 s.  c  o m
        lock.wait(timeout);
    } catch (InterruptedException ie) {
        throw new AssertionError(ie);
    }
}

From source file:Main.java

/**
 * Waits for a object or a timeout for synchronization purposes.
 *//*  w  ww.j  a  va 2s.  co m*/
public static void wait(Object obj, long timeout) {
    synchronized (obj) {
        try {
            obj.wait(timeout);
        } catch (InterruptedException inex) {
            // ignore
        }
    }
}

From source file:Main.java

/**
 * //from w w w.j av  a 2s  .com
 * "waitTimeBool" wait for the given time.
 * 
 * @param object wait() will be start on this given object 
 * @param time
 * @throws InterruptedException when interrupted
 */
public static void waitTimeExcept(Object object, long time) throws InterruptedException {
    synchronized (object) {
        object.wait(time);
    }
}

From source file:Main.java

/**
 * //from   w w  w. j  a  v  a 2  s .  co m
 * "waitTimeBool" wait for the given time.
 * 
 * @param object wait() will be start on this given object
 * @param time
 * @return true if interrupted, else false
 */
public static boolean waitTimeBool(Object object, long time) {
    synchronized (object) {
        try {
            object.wait(time);
        } catch (InterruptedException e) {
            return true;
        }
    }
    return false;
}

From source file:Main.java

public static boolean wait(Object waitObject, long timeout) throws InterruptedException {
    long start = System.currentTimeMillis();
    waitObject.wait(timeout);
    long end = System.currentTimeMillis();
    return (end - start) >= timeout ? false : true;
}