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.buaa.cfs.utils.Shell.java

private static void joinThread(Thread t) {
    while (t.isAlive()) {
        try {/* ww  w . java 2  s.  c  o  m*/
            t.join();
        } catch (InterruptedException ie) {
            if (LOG.isWarnEnabled()) {
                LOG.warn("Interrupted while joining on: " + t, ie);
            }
            t.interrupt(); // propagate interrupt
        }
    }
}

From source file:edu.umass.cs.contextservice.installer.CSInstaller.java

/**
 * Copies the latest version of the JAR files to the all the hosts in the installation given by name and restarts all the servers.
 * Does this using a separate Thread for each host.
 *
 * @param name/*from w w w. j  a  v a 2 s.co m*/
 * @param action
 * @param removeLogs
 * @param deleteDatabase
 * @param lnsHostsFile
 * @param nsHostsFile
 * @param scriptFile
 * @param runAsRoot
 * @param noopTest
 */
public static void updateRunSet(String name, InstallerAction action, boolean deleteDatabase, String scriptFile,
        boolean withGNS) {
    ArrayList<Thread> threads = new ArrayList<>();

    Enumeration<String> keyIter = hostTable.keys();

    while (keyIter.hasMoreElements()) {

        String nodeId = keyIter.nextElement();
        String hostname = hostTable.get(nodeId);
        threads.add(new UpdateThread(hostname, nodeId, action, deleteDatabase, scriptFile, withGNS));
    }

    for (Thread thread : threads) {
        thread.start();
    }
    // and wait for them to complete
    try {
        for (Thread thread : threads) {
            thread.join();
        }
    } catch (InterruptedException e) {
        System.out.println("Problem joining threads: " + e);
    }
    //    if (action != InstallerAction.STOP) {
    //      updateNodeConfigAndSendOutServerInit();
    //    }

    System.out.println("Finished " + name + " " + action.name() + " at " + new Date().toString());
}

From source file:edu.umass.cs.gnsserver.installer.GNSInstaller.java

/**
 * Copies the latest version of the JAR files to the all the hosts in the installation given by name and restarts all the servers.
 * Does this using a separate Thread for each host.
 *
 *
 * @param name//w  w  w. jav a2  s  . co m
 * @param action
 * @param removeLogs
 * @param deleteDatabase
 * @param lnsHostsFile
 * @param nsHostsFile
 * @param scriptFile
 * @param runAsRoot
 * @param noopTest
 */
public static void updateRunSet(String name, InstallerAction action, boolean removeLogs, boolean deleteDatabase,
        String lnsHostsFile, String nsHostsFile, String scriptFile, boolean runAsRoot, boolean noopTest) {
    ArrayList<Thread> threads = new ArrayList<>();
    for (HostInfo info : hostTable.values()) {
        threads.add(new UpdateThread(info.getHostname(), info.getNsId(), noopTest ? false : info.isCreateLNS(),
                action, removeLogs, deleteDatabase, lnsHostsFile, nsHostsFile, scriptFile, runAsRoot,
                noopTest));
    }
    for (Thread thread : threads) {
        thread.start();
    }
    // and wait for them to complete
    try {
        for (Thread thread : threads) {
            thread.join();
        }
    } catch (InterruptedException e) {
        System.out.println("Problem joining threads: " + e);
    }
    System.out
            .println("Finished " + name + " " + action.name() + " at " + Format.formatDateTimeOnly(new Date()));
}

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

/**
 * Adds a shutdown hook to stop the conversion when called.
 * /*from  ww  w . j  ava  2s. co m*/
 * @param textOutputPlugin  the TextOutputPlugin to stop
 */
private static void setupShutdownHook(final TextOutputPlugin textOutputPlugin) {
    logger.debug("TextOutputPlugin.setupShutdownHook() called.");
    final Thread workerThread = Thread.currentThread();

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

From source file:com.android.feedmeandroid.HTTPClient.java

public static Bitmap downloadFile(final String fileUrl) {
    final Bitmap[] bitmap = new Bitmap[1];
    Thread t = new Thread(new Runnable() {
        public void run() {
            URL myFileUrl = null;
            try {
                myFileUrl = new URL(fileUrl);
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();/*from   www .  j  av  a 2s . c o m*/
            }
            try {
                HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
                conn.setDoInput(true);
                conn.connect();
                InputStream is = conn.getInputStream();

                Bitmap bmImg = BitmapFactory.decodeStream(is);
                bitmap[0] = bmImg;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });
    t.start();
    try {
        t.join();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return bitmap[0];
}

From source file:de.micromata.mgc.common.test.ThreadedRunner.java

public void join() {
    try {//from   ww w.  ja  v  a  2s.c  om
        for (Thread t : threads) {
            t.join();
        }
    } catch (InterruptedException ex) {
        throw new RuntimeException(ex);
    }
    long end = System.currentTimeMillis();
    long dif = end - startTime;
    double difPerOpOverall = (double) dif / (threadCount * loops);
    double difPerOp = difPerOpOverall * threadCount;
    System.out.println("Runned threaded  test in " + dif + " ms with " + threadCount + " threads in " + loops
            + " loops. Per op overall: " + difPerOpOverall + " ms" + "; op average: " + difPerOp + " ms");
    if (exeptions.length() > 0) {
        System.err.println(exeptions);
        throw new RuntimeException("One or more Threads failed: " + exeptions);
    }

}

From source file:com.github.paralleltasks.RunParallelTasksTask.java

/**
 * Wait for parallel tasks to complete before executing other tasks
 *
 * @param taskThreads The tasks executing in parallel
 *//*from  w w  w . j  a va 2  s.c  o m*/
protected void waitForCompletionOf(List<Thread> taskThreads) throws InterruptedException {
    for (Thread taskThread : taskThreads) {
        try {
            taskThread.join();
        } catch (InterruptedException ie) {
            getProject().getLogger().error(MarkerFactory.getMarker(ParallelTasksPlugin.PLUGIN_NAME),
                    "Error waiting for completion of thread: {}",
                    ToStringBuilder.reflectionToString(taskThread));
            throw ie;
        }
    }
}

From source file:cz.muni.fi.crocs.JeeTool.UploadMain.java

public void runMake() {
    List<Thread> threads = new ArrayList<Thread>();
    if (cmd.hasOption("m")) {
        try {//from   w  w w  .j  ava  2s.c  om
            ShellExec.verify(projectFile);
        } catch (IOException ex) {
            System.err.println("Error executing verify: " + ex.getMessage());
        }
    } else if (cmd.hasOption("u") && (!cmd.hasOption("t"))) {
        //System.out.println("running make upload");
        for (String motePath : motelist.getMotes().keySet()) {
            Uploadthread r = new Uploadthread(motePath, projectFile, silent);
            r.run();
        }

    } else if (cmd.hasOption("u") && cmd.hasOption("t")) {
        for (String motePath : motelist.getMotes().keySet()) {
            Uploadthread r = new Uploadthread(motePath, projectFile, silent);
            Thread t = new Thread(r);
            t.start();
            threads.add(t);
        }
    }
    for (Thread t1 : threads) {
        try {
            t1.join();
        } catch (InterruptedException ex) {
        }
    }
}

From source file:com.mapd.parser.server.CalciteServerCaller.java

private void doWork(String[] args) {
    CalciteServerWrapper calciteServerWrapper = null;

    // create Options object
    Options options = new Options();

    Option port = Option.builder("p").hasArg().desc("port number").longOpt("port").build();

    Option data = Option.builder("d").hasArg().desc("data directory").required().longOpt("data").build();

    Option extensions = Option.builder("e").hasArg().desc("extension signatures directory")
            .longOpt("extensions").build();

    options.addOption(port);/* w  w  w . j ava 2  s . co  m*/
    options.addOption(data);
    options.addOption(extensions);

    CommandLineParser parser = new DefaultParser();

    try {
        cmd = parser.parse(options, args);
    } catch (ParseException ex) {
        MAPDLOGGER.error(ex.getLocalizedMessage());
        help(options);
        exit(0);
    }

    int portNum = Integer.valueOf(cmd.getOptionValue("port", "9093"));
    String dataDir = cmd.getOptionValue("data", "data");
    String extensionsDir = cmd.getOptionValue("extensions", "build/QueryEngine");
    final Path extensionFunctionsAstFile = Paths.get(extensionsDir, "ExtensionFunctions.ast");

    //Add logging to our log files directories
    Properties p = new Properties();
    try {
        p.load(getClass().getResourceAsStream("/log4j.properties"));
    } catch (IOException ex) {
        MAPDLOGGER.error("Could not load log4j property file from resources " + ex.getMessage());
    }
    p.put("log.dir", dataDir); // overwrite "log.dir"
    PropertyConfigurator.configure(p);

    calciteServerWrapper = new CalciteServerWrapper(portNum, -1, dataDir, extensionFunctionsAstFile.toString());

    while (true) {
        try {
            Thread t = new Thread(calciteServerWrapper);
            t.start();
            t.join();
            if (calciteServerWrapper.shutdown()) {
                break;
            }
        } catch (Exception x) {
            x.printStackTrace();
        }
    }
}

From source file:io.github.mmichaelis.selenium.client.provider.internal.QuitWebDriverRunnableTest.java

@Test
public void quit_webdriver_when_running() throws Exception {
    final Runnable runnable = new QuitWebDriverRunnable(driver);
    final Thread thread = Executors.defaultThreadFactory().newThread(runnable);
    thread.start();//from  w ww.j  a v  a2 s  . c om
    thread.join();
    verify(driver, atLeastOnce()).quit();
}