Example usage for java.lang Thread wait

List of usage examples for java.lang Thread wait

Introduction

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

Prototype

public final void wait() throws InterruptedException 

Source Link

Document

Causes the current thread to wait until it is awakened, typically by being notified or interrupted.

Usage

From source file:Main.java

public final static void waitQuietly(Thread t) {
    wait: do {/*from  w  w w .  j  a  v a 2s .  com*/
        try {
            t.wait();
        } catch (InterruptedException e) {
            continue wait;
        }
    } while (false);
}

From source file:au.com.jwatmuff.eventmanager.util.GUIUtils.java

/**
 * Makes a frame visible and blocks the caller until the frame is closed.
 * //from   w w w.j  a  va2 s. co  m
 * @param frame
 */
public static void runModalJFrame(final JFrame frame) {
    // there may be a much better way of implementing this, i don't know..
    class RunningFlag {
        boolean value = true;
    }

    final RunningFlag flag = new RunningFlag();
    final Thread t = Thread.currentThread();

    try {
        SwingUtilities.invokeAndWait(new Runnable() {
            @Override
            public void run() {
                frame.addWindowListener(new WindowAdapter() {
                    @Override
                    public void windowClosed(WindowEvent arg0) {
                        synchronized (t) {
                            flag.value = false;
                            t.notifyAll();
                        }
                    }
                });

                frame.setVisible(true);

            }
        });

        synchronized (t) {
            while (flag.value == true)
                try {
                    t.wait();
                } catch (InterruptedException e) {
                }
        }
    } catch (InterruptedException e) {
        log.error(e);
    } catch (InvocationTargetException e2) {
        log.error(e2);
    }
}

From source file:org.opennms.netmgt.eventd.adaptors.tcp.TcpStreamHandler.java

/**
 * The main execution context for processing a remote XML document. Once the
 * document is processed and an event receipt is returned to the client the
 * thread will exit.//from ww  w.  ja v  a2s. c om
 */
@Override
public void run() {
    // get the context and stop if necessary
    m_context = Thread.currentThread();
    synchronized (m_context) {
        m_context.notifyAll();
    }

    // check the stop flag
    if (m_stop) {
        LOG.debug("The stop flag was set prior to thread entry, closing connection");
        try {
            m_connection.close();
        } catch (final IOException e) {
            LOG.error("An error occured while closing the connection.", e);
        }

        LOG.debug("Thread context exiting");

        return;
    }

    // Log the startup of this stream handler
    final InetAddress sender = m_connection.getInetAddress();
    LOG.debug("Event Log Stream Handler Started for {}", sender);

    /*
     * This linked list is used to exchange
     * instances of PipedOutputStreams. Whenever a
     * pipe output stream is recovered it must be
     * signaled to inform the EOT thread of the
     * ability to write to the pipe. Also, when
     * the descriptor is close a EOFException is
     * passed on the list.
     */
    final LinkedList<Object> pipeXchange = new LinkedList<Object>();
    final TcpRecordHandler chunker = new TcpRecordHandler(m_connection, pipeXchange);
    final Thread tchunker = new Thread(chunker, "TCPRecord Chunker["
            + InetAddressUtils.str(m_connection.getInetAddress()) + ":" + m_connection.getPort() + "]");
    synchronized (tchunker) {
        tchunker.start();
        try {
            tchunker.wait();
        } catch (final InterruptedException e) {
            LOG.error("The thread was interrupted.", e);
        }
    }

    MAINLOOP: while (!m_stop && m_parent.getStatus() != Fiber.STOP_PENDING
            && m_parent.getStatus() != Fiber.STOPPED && m_recsPerConn != 0) {
        // get a new pipe input stream
        PipedInputStream pipeIn = null;
        synchronized (pipeXchange) {
            while (pipeXchange.isEmpty()) {
                if (chunker.isAlive()) {
                    try {
                        pipeXchange.wait(500);
                    } catch (final InterruptedException e) {
                        LOG.error("The thread was interrupted.", e);
                        break MAINLOOP;
                    }
                } else {
                    break MAINLOOP;
                }
            }

            // if an exception occured then just exit the BAL (Big Ass Loop)
            final Object o = pipeXchange.removeFirst();
            if (o instanceof Throwable) {
                break MAINLOOP;
            }

            // construct the other end of the pipe
            try {
                pipeIn = new PipedInputStream((PipedOutputStream) o);
            } catch (final IOException e) {
                LOG.error("An I/O exception occured construction a record reader.", e);
                break MAINLOOP;
            }

            // signal that we got the stream
            synchronized (o) {
                o.notify();
            }
        }

        // decrement the record count if greater than zero
        m_recsPerConn -= (m_recsPerConn > 0 ? 1 : 0);

        // convert the pipe input stream into a buffered input stream
        final InputStream stream = new BufferedInputStream(pipeIn);

        // Unmarshal the XML document
        Log eLog = null;
        boolean doCleanup = false;
        try {
            eLog = JaxbUtils.unmarshal(Log.class, new InputSource(stream));
            LOG.debug("Event record converted");
        } catch (final Exception e) {
            LOG.error("Could not unmarshall the XML record.", e);
            doCleanup = true;
        } finally {
            if (stream != null) {
                IOUtils.closeQuietly(stream);
            }
        }

        // clean up the data on the current pipe if necessary
        if (doCleanup) {
            /*
             * Cleanup a failed record. Need to read
             * the remaining bytes from the other thread
             * to synchronize up. The other thread might
             * be blocked writing.
             */
            try {
                while (stream.read() != -1) {
                    /* do nothing */;
                }
            } catch (final IOException e) {
                // do nothing
            }

            // start from the top!
            continue MAINLOOP;
        }

        // Now that we have a list of events, process them
        final Event[] events = eLog.getEvents().getEvent();

        // sort the events by time
        Arrays.sort(events, new Comparator<Event>() {
            @Override
            public int compare(final Event e1, final Event e2) {
                final boolean e1t = (e1.getTime() != null);
                final boolean e2t = (e2.getTime() != null);
                if (e1t && !e2t) {
                    return 1;
                } else if (!e1t && e2t) {
                    return -1;
                } else if (!e1t && !e2t) {
                    return 0;
                }

                Date de1 = e1.getTime();
                Date de2 = e2.getTime();

                if (de1 != null && de2 != null) {
                    return (int) (de1.getTime() - de2.getTime());
                } else if (de1 == null && de2 != null) {
                    return -1;
                } else if (de1 != null && de2 == null) {
                    return 1;
                } else {
                    return 0;
                }
            }
        });

        // process the events
        if (events != null && events.length != 0) {
            final List<Event> okEvents = new ArrayList<Event>(events.length);

            /*
             * This synchronization loop will hold onto the lock
             * for a while. If the handlers are going to change
             * often, which is shouldn't then might want to consider
             * duplicating the handlers into an array before processing
             * the events.
             *
             * Doing the synchronization in the outer loop prevents spending
             * lots of cycles doing synchronization when it should not
             * normally be necesary.
             */
            synchronized (m_handlers) {
                for (final EventHandler hdl : m_handlers) {
                    /*
                     * get the handler and then have it process all
                     * the events in the document before moving to the
                     * next event handler.
                     */
                    for (final Event event : events) {
                        /*
                         * Process the event and log any errors,
                         *  but don't die on these errors
                         */
                        try {
                            LOG.debug("handling event: {}", event);

                            // shortcut and BOTH parts MUST execute!
                            if (hdl.processEvent(event)) {
                                if (!okEvents.contains(event)) {
                                    okEvents.add(event);
                                }
                            }
                        } catch (final Throwable t) {
                            LOG.warn("An exception occured while processing an event.", t);
                        }
                    }
                }
            }

            // Now process the good events and send a receipt message
            boolean hasReceipt = false;
            final EventReceipt receipt = new EventReceipt();

            for (final Event event : okEvents) {
                if (event.getUuid() != null) {
                    receipt.addUuid(event.getUuid());
                    hasReceipt = true;
                }
            }

            if (hasReceipt) {
                // Transform it to XML and send it to the socket in one call
                try {
                    final Writer writer = new BufferedWriter(
                            new OutputStreamWriter(m_connection.getOutputStream(), "UTF-8"));
                    JaxbUtils.marshal(receipt, writer);
                    writer.flush();

                    synchronized (m_handlers) {
                        for (final EventHandler hdl : m_handlers) {
                            /*
                             * Get the handler and then have it process all
                             * the events in the document before moving to
                             * the next event hander.
                             */
                            try {
                                hdl.receiptSent(receipt);
                            } catch (final Throwable t) {
                                LOG.warn("An exception occured while processing an event receipt.", t);
                            }
                        }
                    }

                    if (LOG.isDebugEnabled()) {
                        try {
                            final StringWriter swriter = new StringWriter();
                            JaxbUtils.marshal(receipt, swriter);

                            LOG.debug("Sent Event Receipt {");
                            LOG.debug(swriter.getBuffer().toString());
                            LOG.debug("}");
                        } catch (final Throwable e) {
                            LOG.error("An error occured during marshalling of event receipt for the log.", e);
                        }
                    }
                } catch (final IOException e) {
                    LOG.warn("Failed to send event-receipt XML document.", e);
                    break MAINLOOP;
                }
            }
        } else {
            LOG.debug("The agent sent an empty event stream");
        }
    }

    try {
        LOG.debug("stopping record handler");

        chunker.stop();

        LOG.debug("record handler stopped");
    } catch (final InterruptedException e) {
        LOG.warn("The thread was interrupted while trying to close the record handler.", e);
    }

    // regardless of any errors, be sure to release the socket.
    try {
        LOG.debug("closing connnection");

        m_connection.close();

        LOG.debug("connnection closed ");
    } catch (final IOException e) {
        LOG.warn("An I/O exception occured while closing the TCP/IP connection.", e);
    }

    LOG.debug("Thread exiting");
}

From source file:org.rdv.rbnb.RBNBController.java

public boolean connect(boolean block) {
    if (isConnected()) {
        return true;
    }//from  w ww .jav a2  s .c o m

    if (block) {
        final Thread object = Thread.currentThread();
        ConnectionListener listener = new ConnectionListener() {
            public void connecting() {
            }

            public void connected() {
                synchronized (object) {
                    object.notify();
                }
            }

            public void connectionFailed() {
                object.interrupt();
            }
        };
        addConnectionListener(listener);

        synchronized (object) {
            setState(STATE_STOPPED);

            try {
                object.wait();
            } catch (InterruptedException e) {
                return false;
            }
        }

        removeConnectionListener(listener);
    } else {
        setState(STATE_STOPPED);
    }

    return true;
}

From source file:org.rdv.rbnb.RBNBController.java

/**
 * Disconnect from the RBNB server. If block is set, this method will not
 * return until the server has disconnected.
 * //w  ww.  ja  v a  2 s. co m
 * @param block  if true, wait for the server to disconnect
 * @return       true if the server disconnected
 */
public boolean disconnect(boolean block) {
    if (!isConnected()) {
        return true;
    }

    if (block) {
        final Thread object = Thread.currentThread();
        StateListener listener = new StateListener() {
            public void postState(int newState, int oldState) {
                if (newState == STATE_DISCONNECTED) {
                    synchronized (object) {
                        object.notify();
                    }
                }
            }
        };
        addStateListener(listener);

        synchronized (object) {
            setState(STATE_DISCONNECTED);

            try {
                object.wait();
            } catch (InterruptedException e) {
                return false;
            }
        }

        removeStateListener(listener);

    } else {
        setState(STATE_DISCONNECTED);
    }

    return true;
}

From source file:org.talend.commons.utils.threading.Locker.java

/**
 * Method "waitForLock". Lock now if possible with the provided key, else wait for unlocked to lock.
 * /*www .  j  av  a 2s.  co m*/
 * It allows by default reentrant locks from the first locker thread. If you don't want reentrant lock, you have to
 * set <code>allowReentrantLockFromLockerThread</code> = <code>false</code> from the <code>Locker</code>
 * constructor.
 * 
 * @param key
 * @param waitTimeMax the maximum time to wait in milliseconds.
 * @return true if thread has wait a time, else false.
 * @throws InterruptedException
 * @throws IllegalArgumentException if bean is null
 * @deprecated use instead {@link org.talend.commons.utils.threading.lockerbykey.LockerByKey#lockInterruptibly(KP)}
 * or {@link org.talend.commons.utils.threading.lockerbykey.LockerByKey#tryLock(Object, long)} according the case
 * (without or with timeout)
 */
@Deprecated
public boolean waitForLock(KP key, final Long waitTimeMax, String contextInfo) throws InterruptedException {
    check(key);
    if (!lockIfUnlocked(key, contextInfo)) {

        waitingThreadsByKey.put(key, Thread.currentThread());
        try {
            if (log.isTraceEnabled()) {
                log.trace(StringUtils.replacePrms("Waiting for unlocked ({0}) key={1}, contextInfo={2}...", //$NON-NLS-1$
                        Thread.currentThread().getName(), key, contextInfo));
            }
            final Thread mainThread = Thread.currentThread();
            if (waitTimeMax != null) {
                if (treadsPool == null) {
                    initThreadsPool();
                }

                final Thread[] threadInterruptor = new Thread[1];
                final boolean[] threadAlreadyNotified = new boolean[1];

                treadsPool.execute(new Runnable() {

                    @Override
                    public void run() {
                        Thread internalThread = Thread.currentThread();
                        threadInterruptor[0] = internalThread;
                        synchronized (Thread.currentThread()) {
                            try {
                                Thread.currentThread().wait(waitTimeMax);
                                if (!threadAlreadyNotified[0]) {
                                    mainThread.interrupt();
                                }
                            } catch (InterruptedException e) {
                            }
                        }
                    }

                });
                try {
                    synchronized (mainThread) {
                        mainThread.wait();
                        if (threadInterruptor[0] != null) {
                            threadInterruptor[0].interrupt();
                        }
                    }
                } catch (InterruptedException e) {
                    throw e;
                } finally {
                    threadAlreadyNotified[0] = true;
                }
            } else {
                synchronized (mainThread) {
                    mainThread.wait();
                }
            }
            if (log.isTraceEnabled()) {
                log.trace(StringUtils.replacePrms("Waiting ended ({0}) key={1}, contextInfo={2}...", //$NON-NLS-1$
                        Thread.currentThread().getName(), key, contextInfo));
            }
            waitForLock(key, contextInfo);
        } catch (InterruptedException e) {
            throw e;
        } finally {
            waitingThreadsByKey.removeValue(key, Thread.currentThread());
        }
        return true;
    }
    return false;
}

From source file:xbird.engine.remote.RemoteFocusProxy.java

public RemoteInputStream asyncFetch(final int size) throws RemoteException {
    final FastPipedOutputStream pos = new FastPipedOutputStream();
    final FastPipedInputStream pin;
    try {//from  w w w . j  a v  a 2  s .  com
        pin = new FastPipedInputStream(pos);
    } catch (IOException ioe) {
        throw new IllegalStateException(ioe);
    }
    String curThreadName = Thread.currentThread().getName();
    Thread writeThread = new Thread("asyncFetch#" + curThreadName) {
        public void run() {
            try {
                fetchTo(size, pos);
            } catch (IOException e) {
                throw new IllegalStateException("failed to fetch items", e);
            }
        }
    };
    writeThread.start();
    IRemoteInputStreamProxy proxy = new RemoteInputStreamProxy(pin);
    RemoteInputStream remote = new RemoteInputStream(proxy);
    // wait for the first item is obtained
    synchronized (writeThread) {
        try {
            writeThread.wait();
        } catch (InterruptedException e) {
        }
    }
    return remote;
}