Example usage for java.lang Thread join

List of usage examples for java.lang Thread join

Introduction

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

Prototype

public final synchronized void join(final long millis) throws InterruptedException 

Source Link

Document

Waits at most millis milliseconds for this thread to die.

Usage

From source file:io.hops.tensorflow.ApplicationMaster.java

private boolean finish() {
    // wait for completion. finish if any container fails
    while (!done && !(numCompletedWorkers.get() == numWorkers) && !(numFailedContainers.get() > 0)) {
        if (numAllocatedContainers.get() != numTotalContainers) {
            long timeLeft = appMasterStartTime + allocationTimeout - System.currentTimeMillis();
            LOG.info("Awaits container allocation, timeLeft=" + timeLeft);
            if (timeLeft < 0) {
                LOG.warn("Container allocation timeout. Finish application attempt");
                break;
            }// w  ww  .  j a  v  a2 s  .  com
        }
        try {
            Thread.sleep(200);
        } catch (InterruptedException ex) {
        }
    }

    if (timelineHandler.isClientNotNull()) {
        timelineHandler.publishApplicationAttemptEvent(YarntfEvent.YARNTF_APP_ATTEMPT_END);
    }

    // Join all launched threads
    // needed for when we time out
    // and we need to release containers
    for (Thread launchThread : launchThreads) {
        try {
            launchThread.join(10000);
        } catch (InterruptedException e) {
            LOG.info("Exception thrown in thread join: " + e.getMessage());
            e.printStackTrace();
        }
    }

    // When the application completes, it should stop all running containers
    LOG.info("Application completed. Stopping running containers");
    nmWrapper.getClient().stop();

    // When the application completes, it should send a finish application
    // signal to the RM
    LOG.info("Application completed. Signalling finish to RM");

    FinalApplicationStatus appStatus;
    String appMessage = null;
    boolean success = true;
    if (numFailedContainers.get() == 0 && numCompletedWorkers.get() == numWorkers) {
        appStatus = FinalApplicationStatus.SUCCEEDED;
    } else {
        appStatus = FinalApplicationStatus.FAILED;
        appMessage = "Diagnostics." + ", total=" + numTotalContainers + ", completed(workers)="
                + numCompletedContainers.get() + "(" + numCompletedWorkers.get() + "), allocated="
                + numAllocatedContainers.get() + ", failed=" + numFailedContainers.get();
        LOG.info(appMessage);
        success = false;
    }
    try {
        rmWrapper.getClient().unregisterApplicationMaster(appStatus, appMessage, null);
    } catch (YarnException ex) {
        LOG.error("Failed to unregister application", ex);
    } catch (IOException e) {
        LOG.error("Failed to unregister application", e);
    }

    rmWrapper.getClient().stop();

    // Stop Timeline Client
    if (timelineHandler.isClientNotNull()) {
        timelineHandler.stopClient();
    }

    return success;
}

From source file:ezbake.security.common.core.FileWatcherTest.java

@Test
public void testEvents() throws IOException, InterruptedException {
    final String contents = "TESTSTSETSTSTETSTSETST";

    FileWatcher w = new FileWatcher(Paths.get(folder.getRoot() + "/testFile"),
            new FileWatcher.FileWatchUpdater() {
                @Override//from w  ww  . j  a  va2  s .c o  m
                public boolean loadUpdate(InputStream is) {
                    try {
                        byte[] bytes = new byte[contents.length()];
                        is.read(bytes, 0, contents.length());
                        logger.debug("Read bytes {}", bytes);
                        Assert.assertEquals(contents, new String(bytes));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    return false;
                }

                @Override
                public void close() throws IOException {

                }
            });
    Thread watchThread = new Thread(w);
    watchThread.start();

    Thread.sleep(50);

    File f2 = folder.newFile("otherFile");
    try (PrintWriter writer = new PrintWriter(Files.newBufferedWriter(f2.toPath(), Charset.defaultCharset()))) {
        writer.println("HTESTS");
    }
    try (PrintWriter writer = new PrintWriter(Files.newBufferedWriter(f2.toPath(), Charset.defaultCharset()))) {
        writer.println("HTElkajsldkfjSTS");
    }

    File watchPath = folder.newFile("testFile");
    try (PrintWriter writer = new PrintWriter(
            Files.newBufferedWriter(watchPath.toPath(), Charset.defaultCharset()))) {
        writer.println(contents);
    }

    watchThread.join(10 * 1000);
}

From source file:com.github.hdl.tensorflow.yarn.app.ApplicationMaster.java

@VisibleForTesting
protected boolean finish() {
    // wait for completion.
    while (!done && (numCompletedContainers.get() != numTotalContainers)) {
        try {// w w  w  . ja v  a2 s.c o m
            Thread.sleep(200);
        } catch (InterruptedException ex) {
        }
    }

    // Join all launched threads
    // needed for when we time out
    // and we need to release containers
    for (Thread launchThread : launchThreads) {
        try {
            launchThread.join(10000);
        } catch (InterruptedException e) {
            LOG.info("Exception thrown in thread join: " + e.getMessage());
            e.printStackTrace();
        }
    }

    // When the application completes, it should stop all running containers
    LOG.info("Application completed. Stopping running containers");
    nmClientAsync.stop();

    // When the application completes, it should send a finish application
    // signal to the RM
    LOG.info("Application completed. Signalling finish to RM");

    FinalApplicationStatus appStatus;
    String appMessage = null;
    boolean success = true;
    if (numCompletedContainers.get() - numFailedContainers.get() >= numTotalContainers) {
        appStatus = FinalApplicationStatus.SUCCEEDED;
    } else {
        appStatus = FinalApplicationStatus.FAILED;
        appMessage = "Diagnostics." + ", total=" + numTotalContainers + ", completed="
                + numCompletedContainers.get() + ", allocated=" + numAllocatedContainers.get() + ", failed="
                + numFailedContainers.get();
        LOG.info(appMessage);
        success = false;
    }
    try {
        amRMClient.unregisterApplicationMaster(appStatus, appMessage, null);
    } catch (YarnException ex) {
        LOG.error("Failed to unregister application", ex);
    } catch (IOException e) {
        LOG.error("Failed to unregister application", e);
    }

    amRMClient.stop();

    return success;
}

From source file:net.greghaines.jesque.worker.WorkerImpl.java

/**
 * {@inheritDoc}//  www . ja va 2s.  c  o  m
 */
@Override
public void join(final long millis) throws InterruptedException {
    final Thread workerThread = this.threadRef.get();
    if (workerThread != null && workerThread.isAlive()) {
        workerThread.join(millis);
    }
}

From source file:com.workplacesystems.queuj.utils.BackgroundProcess.java

/**
 * Inactivively waits for the process to finish or until timeout occurs,
 * whichever is earlier. If the process was not running, returns immediatelly.
 * //from ww  w .  j  a v  a 2s . c  om
 * @param timeout
 */
public void joinThread(long timeout) {
    // take local ref copy, so can reliably check if ref is null,
    Thread local_thread_ref = thread;
    if (local_thread_ref != null) {
        try {
            long millis = 0l;
            if (log.isDebugEnabled()) {
                log.debug("BackgroundProcess.joinThread() Waiting " + timeout + " millis for " + this
                        + " to finish");
                millis = System.currentTimeMillis();
            }
            if (thread_pool != null) {
                synchronized (pool_mutex) {
                    pool_mutex.wait(timeout);
                }
            } else
                local_thread_ref.join(timeout);

            if (log.isDebugEnabled()) {
                millis = System.currentTimeMillis() - millis;
                log.debug("BackgroundProcess.joinThread() Process " + this + " finished in " + millis
                        + " millis");
            }
        } catch (InterruptedException e) {
            if (log.isDebugEnabled())
                log.debug("BackgroundProcess.joinThread() Timed out waiting " + timeout + " millis for " + this
                        + " to finish");
        }
    } else {
        if (log.isDebugEnabled())
            log.debug("BackgroundProcess.joinThread() " + name + " was not running, thread was null");
    }
}

From source file:com.bigjob.ApplicationMaster.java

private void finish() {
    // Join all launched threads
    // needed for when we time out
    // and we need to release containers
    for (Thread launchThread : launchThreads) {
        try {//from w w w .j  av a 2s.  c o  m
            launchThread.join(10000);
        } catch (InterruptedException e) {
            LOG.info("Exception thrown in thread join: " + e.getMessage());
            e.printStackTrace();
        }
    }

    // When the application completes, it should stop all running containers
    LOG.info("Application completed. Stopping running containers");
    nmClientAsync.stop();

    // When the application completes, it should send a finish application
    // signal to the RM
    LOG.info("Application completed. Signalling finish to RM");

    FinalApplicationStatus appStatus;
    String appMessage = null;
    success = true;
    if (numFailedContainers.get() == 0 && numCompletedContainers.get() == numTotalContainers) {
        appStatus = FinalApplicationStatus.SUCCEEDED;
    } else {
        appStatus = FinalApplicationStatus.FAILED;
        appMessage = "Diagnostics." + ", total=" + numTotalContainers + ", completed="
                + numCompletedContainers.get() + ", allocated=" + numAllocatedContainers.get() + ", failed="
                + numFailedContainers.get();
        success = false;
    }
    try {
        amRMClient.unregisterApplicationMaster(appStatus, appMessage, null);
    } catch (YarnException ex) {
        LOG.error("Failed to unregister application", ex);
    } catch (IOException e) {
        LOG.error("Failed to unregister application", e);
    }

    amRMClient.stop();
}

From source file:com.zqh.hadoop.moya.core.yarn.ApplicationMaster.java

private void finish() {
    // Join all launched threads
    // needed for when we time out
    // and we need to release containers
    for (Thread launchThread : launchThreads) {
        try {//from w w w.  j  a va 2 s  .co m
            launchThread.join(10000);
        } catch (InterruptedException e) {
            LOG.info("Exception thrown in thread join: " + e.getMessage());
            e.printStackTrace();
        }
    }

    // When the application completes, it should stop all running containers
    LOG.info("Application completed. Stopping running containers");
    nmClientAsync.stop();

    // When the application completes, it should send a finish application
    // signal to the RM
    LOG.info("Application completed. Signalling finish to RM");

    //TODO Remove MOYA NODE
    try {
        DeleteGroup.main(new String[] { ZKHosts, "moya" });
    } catch (Exception e1) {
        e1.printStackTrace();
    }

    FinalApplicationStatus appStatus;
    String appMessage = null;
    success = true;
    if (numFailedContainers.get() == 0 && numCompletedContainers.get() == numTotalContainers) {
        appStatus = FinalApplicationStatus.SUCCEEDED;
    } else {
        appStatus = FinalApplicationStatus.FAILED;
        appMessage = "Diagnostics." + ", total=" + numTotalContainers + ", completed="
                + numCompletedContainers.get() + ", allocated=" + numAllocatedContainers.get() + ", failed="
                + numFailedContainers.get();
        success = false;
    }
    try {
        resourceManager.unregisterApplicationMaster(appStatus, appMessage, null);
    } catch (YarnException ex) {
        LOG.error("Failed to unregister application", ex);
    } catch (IOException e) {
        LOG.error("Failed to unregister application", e);
    }

    done = true;
    resourceManager.stop();
}

From source file:edu.umn.cs.spatialHadoop.core.GridRecordWriter.java

/**
 * Close the whole writer. Finalize all cell files and concatenate them
 * into the output file./*from  w  w  w.  j av a2 s  .  c  o  m*/
 */
public synchronized void close(Progressable progressable) throws IOException {
    // Close all output files
    for (int cellIndex = 0; cellIndex < intermediateCellStreams.length; cellIndex++) {
        if (intermediateCellStreams[cellIndex] != null) {
            closeCell(cellIndex);
        }
        // Indicate progress. Useful if closing a single cell takes a long time
        if (progressable != null)
            progressable.progress();
    }
    LOG.info("Closing record writer with " + closingThreads.size() + " remaining threads");

    while (!closingThreads.isEmpty()) {
        try {
            Thread t = closingThreads.get(0);
            switch (t.getState()) {
            case NEW:
                t.start();
                break;
            case TERMINATED:
                closingThreads.remove(0);
                break;
            default:
                // Use limited time join to indicate progress frequently
                t.join(10000);
            }
            // Indicate progress. Useful if closing a single cell takes a long time
            if (progressable != null)
                progressable.progress();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    if (masterFile != null)
        masterFile.close();
}

From source file:cn.edu.buaa.act.petuumOnYarn.ApplicationMaster.java

@VisibleForTesting
protected boolean finish() {
    // wait for completion.
    while (!done && (numCompletedContainers.get() != numTotalContainers)) {
        try {// w w  w.j  a v  a2  s.  c  o  m
            Thread.sleep(200);
        } catch (InterruptedException ex) {
        }
    }

    // Join all launched threads
    // needed for when we time out
    // and we need to release containers
    for (Thread launchThread : launchThreads) {
        try {
            launchThread.join(10000);
        } catch (InterruptedException e) {
            LOG.info("Exception thrown in thread join: " + e.getMessage());
            e.printStackTrace();
        }
    }

    // When the application completes, it should stop all running containers
    LOG.info("Application completed. Stopping running containers");
    nmClientAsync.stop();

    // When the application completes, it should send a finish application
    // signal to the RM
    LOG.info("Application completed. Signalling finish to RM");

    FinalApplicationStatus appStatus;
    String appMessage = null;
    boolean success = true;
    if (numFailedContainers.get() == 0 && numCompletedContainers.get() == numTotalContainers) {
        appStatus = FinalApplicationStatus.SUCCEEDED;
    } else {
        appStatus = FinalApplicationStatus.FAILED;
        appMessage = "Diagnostics." + ", total=" + numTotalContainers + ", completed="
                + numCompletedContainers.get() + ", allocated=" + numAllocatedContainers.get() + ", failed="
                + numFailedContainers.get();
        LOG.info(appMessage);
        success = false;
    }
    try {
        amRMClient.unregisterApplicationMaster(appStatus, appMessage, null);
    } catch (YarnException ex) {
        LOG.error("Failed to unregister application", ex);
    } catch (IOException e) {
        LOG.error("Failed to unregister application", e);
    }

    amRMClient.stop();

    return success;
}

From source file:com.sogou.dockeronyarn.client.DockerApplicationMaster_23.java

private void finish() {
    // Join all launched threads
    // needed for when we time out
    // and we need to release containers
    for (Thread launchThread : launchThreads) {
        try {/* www. j  a  va 2s . c  o m*/
            launchThread.join(10000);
        } catch (InterruptedException e) {
            LOG.info("Exception thrown in thread join: " + e.getMessage());
            e.printStackTrace();
        }
    }

    // When the application completes, it should stopContainer all running containers
    LOG.info("Application completed. Stopping running containers");
    nmClientAsync.stop();

    // When the application completes, it should send a shutdown application
    // signal to the RM
    LOG.info("Application completed. Signalling shutdown to RM");

    FinalApplicationStatus appStatus;
    String appMessage = null;
    success = true;
    if (numFailedContainers.get() == 0 && numCompletedContainers.get() == numTotalContainers) {
        appStatus = FinalApplicationStatus.SUCCEEDED;
    } else {
        appStatus = FinalApplicationStatus.FAILED;
        appMessage = "Diagnostics." + ", total=" + numTotalContainers + ", completed="
                + numCompletedContainers.get() + ", allocated=" + numAllocatedContainers.get() + ", failed="
                + numFailedContainers.get();
        success = false;
    }
    try {
        amRMClient.unregisterApplicationMaster(appStatus, appMessage, null);
    } catch (YarnException ex) {
        LOG.error("Failed to unregister application", ex);
    } catch (IOException e) {
        LOG.error("Failed to unregister application", e);
    }

    amRMClient.stop();
}