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 void join() throws InterruptedException 

Source Link

Document

Waits for this thread to die.

Usage

From source file:com.seajas.search.concept.QueuePerformanceTest.java

@Test
public void spool() throws Exception {
    final int threadCount = 4;
    final int messageCount = 20000;
    final Message<String> message = MessageBuilder.withPayload("Hello World!").build();

    Thread[] threads = new Thread[threadCount];
    for (int i = 0; i < threadCount; ++i) {
        threads[i] = new Thread() {
            @Override//from w ww.  j a v a2 s .c o m
            public void run() {
                for (int i = messageCount / threadCount; i > 0; --i) {
                    if (i % 1000 == 0)
                        logger.debug("Remaining: " + i);
                    writeChannel.send(message);
                }
            }
        };
    }

    long before = System.currentTimeMillis();
    for (Thread t : threads)
        t.start();
    for (Thread t : threads)
        t.join();
    long elapsed = System.currentTimeMillis() - before;

    String reportFormat = "%d messages took %fs with %d threads (%fms per message).";
    logger.info(String.format(reportFormat, messageCount, elapsed / 1000.0, threadCount,
            (double) elapsed / messageCount));
}

From source file:io.osv.LoggingWithoutIsolationTest.java

@Test
public void testLogger() throws Throwable {
    File log = newTemporaryFile();

    Thread thread = runWithoutIsolation(NonIsolatedLoggingProcess.class, log.getAbsolutePath(), LOGGER_NAME,
            "INFO", "ctx");
    thread.join();

    ///*from  ww w .ja va 2  s  .  c om*/
    // Rethrow any exception that may have been raised and led to the thread terminating
    final Throwable exception = NonIsolatedJvm.getInstance().getThrownExceptionIfAny();
    if (null != exception)
        throw exception;

    final List<String> logLines = readLines(log);
    for (String line : logLines)
        System.out.println(line);

    assertThat(logLines).hasSize(4).contains("INFO: ctx").contains("WARNING: ctx");
}

From source file:org.apache.hyracks.http.test.HttpServerTest.java

@Test
public void testOverloadingServer() throws Exception {
    WebManager webMgr = new WebManager();
    HttpServer server = new HttpServer(webMgr.getBosses(), webMgr.getWorkers(), PORT, NUM_EXECUTOR_THREADS,
            SERVER_QUEUE_SIZE);//from w ww.  ja va2  s .c  o m
    SlowServlet servlet = new SlowServlet(server.ctx(), new String[] { PATH });
    server.addServlet(servlet);
    webMgr.add(server);
    webMgr.start();
    try {
        request(NUM_OF_REQUESTS);
        for (Thread thread : THREADS) {
            thread.join();
        }
        Assert.assertEquals(32, SUCCESS_COUNT.get());
        Assert.assertEquals(16, UNAVAILABLE_COUNT.get());
        Assert.assertEquals(0, OTHER_COUNT.get());
    } finally {
        webMgr.stop();
    }
}

From source file:edu.hawaii.soest.kilonalu.utilities.FileArchiverSink.java

/**
 * Adds a shutdown hook to stop the export when called.
 * /*  ww  w. j a  va  2  s  .c o  m*/
 * @param fileArchiverSink  the FileArchiverSink to stop
 */
private static void setupShutdownHook(final FileArchiverSink fileArchiverSink) {
    logger.debug("FileArchiverSink.setupShutdownHook() called.");
    final Thread workerThread = Thread.currentThread();

    Runtime.getRuntime().addShutdownHook(new Thread() {
        public void run() {
            fileArchiverSink.stopExport();
            try {
                workerThread.join();
            } catch (InterruptedException e) {
            }
        }
    });
}

From source file:com.asakusafw.bulkloader.transfer.StreamFileListProvider.java

@Override
public final void waitForComplete() throws IOException, InterruptedException {
    synchronized (running) {
        for (Iterator<Thread> iter = running.iterator(); iter.hasNext();) {
            Thread next = iter.next();
            if (next.isAlive()) {
                next.join();
            }/*from   www . j ava2 s .c o  m*/
            iter.remove();
        }
    }
    waitForDone();
}

From source file:com.espertech.esper.multithread.TestMTStmtStateless.java

private void trySend(int numThreads, int numRepeats) throws Exception {
    engine.getEPAdministrator().getConfiguration().addEventType(SentenceEvent.class);
    EPStatementSPI spi = (EPStatementSPI) engine.getEPAdministrator()
            .createEPL("select * from SentenceEvent[words]");
    assertTrue(spi.getStatementContext().isStatelessSelect());

    StatelessRunnable[] runnables = new StatelessRunnable[numThreads];
    for (int i = 0; i < runnables.length; i++) {
        runnables[i] = new StatelessRunnable(engine, numRepeats);
    }/*ww  w  .jav a  2  s  .  co  m*/

    Thread[] threads = new Thread[numThreads];
    for (int i = 0; i < runnables.length; i++) {
        threads[i] = new Thread(runnables[i]);
    }

    long start = System.currentTimeMillis();
    for (Thread t : threads) {
        t.start();
    }

    for (Thread t : threads) {
        t.join();
    }
    long delta = System.currentTimeMillis() - start;
    log.info("Delta=" + delta + " for " + numThreads * numRepeats + " events");

    for (StatelessRunnable r : runnables) {
        assertNull(r.getException());
    }
}

From source file:com.splunk.shuttl.archiver.retry.ColdToFrozenRetrierTest.java

private void stopRetrier(Thread thread, ColdToFrozenRetrier retrier) throws InterruptedException {
    thread.join();
}

From source file:net.sf.jabref.JabRefExecutorService.java

private void waitForThreadToFinish(Thread thread) {
    while (true) {
        try {//w  w  w .  jav  a  2  s  .  c om
            thread.join();
            startedThreads.remove(thread);
            return;
        } catch (InterruptedException ignored) {
            // Ignored
        }
    }
}

From source file:com.nbzs.ningbobus.ui.loaders.BusRunInfoReaderLoader.java

public List<BusLineRunInfoItem> loadInBackground() {
    Log.d(TAG, "loading URI: " + mFeedUri);

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.getContext());
    String selectedBusLines = prefs.getString("SelectedBusLines", "");
    String[] selected = selectedBusLines.split(",");
    List<Integer> busLines = new ArrayList<Integer>();
    for (int i = 0; i < selected.length; ++i) {
        try {//from  ww  w. j a  v a2 s  . c o  m
            busLines.add(Integer.parseInt(selected[i]));
        } catch (NumberFormatException e) {
        }
    }

    Calendar cal = Calendar.getInstance();
    cal.getTime();
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
    Log.i(TAG, "start read feed at" + sdf.format(cal.getTime()));

    final List<BusLineRunInfoItem> items = Collections.synchronizedList(new ArrayList<BusLineRunInfoItem>());

    Thread[] threads = new Thread[busLines.size()];
    for (int i = 0; i < busLines.size(); ++i) {
        final int idx = busLines.get(i);
        threads[i] = new Thread(new Runnable() {
            public void run() {
                BusLineRunInfoItem item = downloadOneFeedItem(idx);
                synchronized (items) {
                    if (item != null) {
                        items.add(item);
                    }
                }
            }
        });
        threads[i].start();
    }
    try {
        for (Thread thread : threads) {
            thread.join();
        }
    } catch (InterruptedException e) {
        Log.d(TAG, "this should not happen");
        Log.d(TAG, Log.getStackTraceString(e));
    }
    cal.getTime();
    Log.i(TAG, "end read feed at" + sdf.format(cal.getTime()));
    return items;
}

From source file:de.micromata.genome.util.matcher.GenomeCommonsThreadedRunner.java

public void run(final RuntimeCallable caller) {
    long start = System.currentTimeMillis();
    Thread[] threads = new Thread[threadCount];
    for (int i = 0; i < threadCount; ++i) {
        final int intPrefix = i;
        threads[i] = new Thread(new Runnable() {

            @Override//from ww w. j  a v a 2s  . co  m
            public void run() {
                try {
                    for (int i = 0; i < loops; ++i) {
                        synchronized (GenomeCommonsThreadedRunner.this) {
                            if (exeptions.length() > 0) {
                                break;
                            }
                        }
                        caller.call();
                    }
                } catch (Exception ex) {
                    synchronized (GenomeCommonsThreadedRunner.this) {
                        exeptions.append("\n\nThread ").append(Thread.currentThread().getId())
                                .append(" faield:\n").append(ExceptionUtils.getStackTrace(ex));
                    }
                }
            }

        });
    }
    for (Thread t : threads) {
        t.start();
    }
    try {
        for (Thread t : threads) {
            t.join();
        }
    } catch (InterruptedException ex) {
        throw new RuntimeException(ex);
    }
    long end = System.currentTimeMillis();
    long dif = end - start;
    double difPerOp = (double) dif / (threadCount * loops);
    System.out.println("Runned threaded  test in " + dif + " ms with " + threadCount + " threads in " + loops
            + " loops. Per op: " + difPerOp + " ms");
    if (exeptions.length() > 0) {
        System.err.println(exeptions);
        throw new RuntimeException("One or more Threads failed: " + exeptions);
    }
}