Example usage for java.lang Thread notify

List of usage examples for java.lang Thread notify

Introduction

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

Prototype

@HotSpotIntrinsicCandidate
public final native void notify();

Source Link

Document

Wakes up a single thread that is waiting on this object's monitor.

Usage

From source file:net.bluehornreader.service.Service.java

public void waitForThreadsToFinish() {
    long start = System.currentTimeMillis();
    for (;;) {// w ww  . j  a  v  a  2s.  co m
        boolean foundAlive = false;
        if (isAlive()) {
            foundAlive = true;
            LOG.info(String.format("Thread %s is still alive", getName()));
            synchronized (this) {
                notify();
            }
        }
        for (Thread thread : getChildThreads()) {
            if (thread.isAlive()) {
                foundAlive = true;
                LOG.info(String.format("Thread %s is still alive", thread.getName()));
                synchronized (thread) { //ttt2 make sure this works, as Idea complains: create a separate test case; (the thing is that the object is not
                    // local, but taken from a global collection)
                    thread.notify();
                }
            }
        }
        if (foundAlive) {
            try {
                if (System.currentTimeMillis() - start > 10000) { // ttt2 maybe make configurable
                    LOG.warn("Some threads are still running. Giving up on shutting them down.");
                    return;
                }

                Thread.sleep(1000);
            } catch (InterruptedException e) {
                LOG.error("Exception sleeping while trying to shut down child threads", e);
            }
        } else {
            return;
        }
    }
}

From source file:com.clican.pluto.dataprocess.engine.processes.TimerProcessor.java

public void process(final ProcessorContext context) throws DataProcessException {
    String id = UUID.randomUUID().toString();
    Date start = DateUtils.truncate(new Date(), Calendar.DAY_OF_MONTH);
    Date end = DateUtils.truncate(new Date(), Calendar.DAY_OF_MONTH);
    SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");
    final SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
    final List<Throwable> exceptionList = new ArrayList<Throwable>();
    final Thread currentThread = Thread.currentThread();
    try {/* w w w  .j a v a2  s.  c  o  m*/
        start = sdf2.parse(sdf1.format(start) + " " + startTime.trim());
        end = sdf2.parse(sdf1.format(end) + " " + endTime.trim());
        ScheduledTask task = new ScheduledTask(new Runnable() {

            public void run() {
                log.debug("timerProcessor?,?" + sdf2.format(new Date()));
                try {
                    for (DataProcessor timerProcessor : timerProcessors) {
                        if (stepCommit) {
                            dataProcessTransaction.doInCommit(timerProcessor, context);
                        } else {
                            timerProcessor.beforeProcess(context);
                            timerProcessor.process(context);
                            timerProcessor.afterProcess(context);
                        }
                    }
                } catch (InterruptedException e) {
                    log.error("Timer", e);
                    exceptionList.add(e);
                    synchronized (currentThread) {
                        currentThread.notify();
                    }
                } catch (Throwable e) {
                    log.error("Timer", e);
                    exceptionList.add(e);
                }
                log.debug("timerProcessor?,?" + sdf2.format(new Date()));
            }
        }, id, cronExpression, start, end, concurrent);
        long timeout = end.getTime() - new Date().getTime();
        if (log.isDebugEnabled()) {
            log.debug("timeout=" + timeout);
        }
        long beforetime = start.getTime() - new Date().getTime();
        if (beforetime > 5000) {
            throw new InterruptedException("???task,beforetime=[" + beforetime + "]");
        }
        if (timeout < 0) {
            throw new InterruptedException("????task,timeout=[" + timeout + "]");
        } else {
            taskScheduler.schedule(id, task);
            synchronized (currentThread) {
                currentThread.wait(timeout);
            }
        }
    } catch (Exception e) {
        throw new DataProcessException("Timer[" + this.getId() + "]", e);
    }
    try {
        if (exceptionList.size() != 0) {
            throw new DataProcessException(exceptionList.get(0));
        }
    } catch (Exception e) {
        throw new DataProcessException("TimerProcessor[" + this.getId() + "]", e);
    } finally {
        taskScheduler.cancel(id);
    }
}

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

public boolean connect(boolean block) {
    if (isConnected()) {
        return true;
    }//from   w  ww . j  ava  2s.  co 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.
 * //from  w ww . j av 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;
}