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:edu.iu.harp.schdynamic.ComputeUtil.java

public static void joinThread(Thread thread) {
    boolean isFailed = false;
    do {// w  w  w.j  av  a 2 s  . c o  m
        isFailed = false;
        try {
            thread.join();
        } catch (Exception e) {
            LOG.error("Error when joining thread.", e);
            isFailed = true;
        }
    } while (isFailed);
}

From source file:Main.java

/** Start all given threads and wait on each of them until all are done.
 * From Stephan Preibisch's Multithreading.java class. See:
 * http://repo.or.cz/w/trakem2.git?a=blob;f=mpi/fruitfly/general/MultiThreading.java;hb=HEAD
 * @param threads //  w ww. j a va 2  s. c  o  m
 */
public static void startAndJoin(Thread[] threads) {
    for (Thread thread : threads) {
        thread.setPriority(Thread.NORM_PRIORITY);
        thread.start();
    }

    try {
        for (Thread thread : threads) {
            thread.join();
        }
    } catch (InterruptedException ie) {
        throw new RuntimeException(ie);
    }
}

From source file:com.sshtools.common.util.ShutdownHooks.java

public static synchronized void exit(final boolean exit) {
    Iterator ifiles = files.iterator();
    while (ifiles.hasNext()) {
        String file = (String) ifiles.next();
        File f = new File(file);
        if (f != null & f.exists()) {
            f.delete();//from w w w  .j  a v a2 s.  co  m
        }
    }
    Iterator icodes = codes.iterator();
    Notif n = new Notif(codes.size());
    while (icodes.hasNext()) {
        Runnable code = (Runnable) icodes.next();
        try {
            Thread t = new Helper(code, n);
            t.start();
            if (!exit)
                t.join();
        } catch (IllegalThreadStateException e) {
            log.error("Exception", e);
        } catch (InterruptedException e) {
            log.error("Exception", e);
        }
    }

    files = new LinkedList();
    codes = new LinkedList();

    if (exit)
        n.start();
}

From source file:com.adaptris.transport.JunitSocketServer.java

private static void join(Thread t) {
    try {/*ww  w .j  ava 2s  .co m*/
        if (t.isAlive()) {
            t.join();
        }
    } catch (InterruptedException ignored) {
        logR.warn("Interrupted");
    }
}

From source file:net.dryuf.concurrent.benchmark.BenchmarkSupport.java

public static void threadedRunFutures(RunnableFuture<Integer>[] array) {
    Thread t = new Thread(() -> {
        for (RunnableFuture<Integer> v : array) {
            v.run();//from  ww  w .j a  v  a 2 s .c  o  m
        }
    });
    t.start();
    try {
        t.join();
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static void sleep(int milliseconds) {
    final int ms = milliseconds;
    Thread t = new Thread() {
        public void run() {
            try {
                sleep(ms);//from w  ww  .  ja v  a  2  s  . c  o m
            } catch (Exception e) {
            }
        }
    };
    t.start();
    try {
        t.join();
    } catch (Exception e) {
    }
}

From source file:Main.java

private static void runTest() throws InterruptedException {
    Collection<String> syncList = Collections.synchronizedList(new ArrayList<String>(1));
    List<String> list = Arrays.asList("A", "B", "C", "D");
    List<Thread> threads = new ArrayList<Thread>();
    for (final String s : list) {
        Thread thread = new Thread(new Runnable() {
            @Override/* w  w  w. jav a 2  s .  c  o  m*/
            public void run() {
                syncList.add(s);
            }
        });
        threads.add(thread);
        thread.start();
    }
    for (Thread thread : threads) {
        thread.join();
    }
    System.out.println(syncList);
}

From source file:com.espertech.esper.regression.nwtable.TestTableMTGroupedWContextIntoTableWriteAsSharedTable.java

protected static void runAndAssert(EPServiceProvider epService, String eplDeclare, String eplAssert,
        int numThreads, int numLoops, int numGroups) throws Exception {
    epService.getEPAdministrator().getDeploymentAdmin().parseDeploy(eplDeclare);

    // setup readers
    Thread[] writeThreads = new Thread[numThreads];
    WriteRunnable[] writeRunnables = new WriteRunnable[numThreads];
    for (int i = 0; i < writeThreads.length; i++) {
        writeRunnables[i] = new WriteRunnable(epService, numLoops, numGroups);
        writeThreads[i] = new Thread(writeRunnables[i]);
    }/*from w  w w  . j a  va2s  .  com*/

    // start
    for (Thread writeThread : writeThreads) {
        writeThread.start();
    }

    // join
    log.info("Waiting for completion");
    for (Thread writeThread : writeThreads) {
        writeThread.join();
    }

    // assert
    for (WriteRunnable writeRunnable : writeRunnables) {
        assertNull(writeRunnable.getException());
    }

    // each group should total up to "numLoops*numThreads"
    SupportUpdateListener listener = new SupportUpdateListener();
    epService.getEPAdministrator().createEPL(eplAssert).addListener(listener);
    Integer expected = numLoops * numThreads;
    for (int i = 0; i < numGroups; i++) {
        epService.getEPRuntime().sendEvent(new SupportBean_S0(0, "G" + i));
        assertEquals(expected, listener.assertOneGetNewAndReset().get("c0"));
    }
}

From source file:io.fabric8.maven.core.util.ProcessUtil.java

private static void joinThreads(List<Thread> threads, Logger log) {
    for (Thread thread : threads) {
        try {//from w  w w  .  j  av  a2 s  .c  o  m
            thread.join();
        } catch (InterruptedException e) {
            log.warn("Caught %s", e);
        }
    }
}

From source file:com.cueup.hegemon.TestUtils.java

public static void runConcurrent(int count, Runnable r) throws InterruptedException {
    List<Thread> threads = Lists.newArrayList();
    ErrorCollector errorCollector = new ErrorCollector();

    for (int i = 0; i < count; i++) {
        Thread t = new Thread(r);
        t.setName("testThread" + i);
        t.setUncaughtExceptionHandler(errorCollector);
        threads.add(t);/*  ww w  .j  a  v a  2 s  . c  o  m*/

    }

    for (Thread t : threads) {
        t.start();
    }

    for (Thread t : threads) {
        t.join();
    }

    errorCollector.assertNoErrors();
}