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:core.Inject.java

public void checkElements(String injection) {

    if (Starter.getParsedArgs().getDelayInterval() == 0) {
        // 5 threads are started in order to address the reversion of the filter
        // if a delay is setted, then a sequential approach will be adopted
        int partitions_size = 25;
        int i = 0, j = 0;
        String[] partition_a = new String[partitions_size];
        String[] partition_b = new String[partitions_size];
        String[] partition_c = new String[partitions_size];
        String[] partition_d = new String[partitions_size];
        String[] partition_e = new String[htmlElements.length - (partitions_size * 4)];
        String attributes = "";

        for (String attribute : htmlAttributes) {
            attributes += attribute + "=\"X\" ";
        }/* w w w . j a va 2  s .  c  o m*/

        for (String element : htmlElements) {
            if (j >= partitions_size && i <= 4 * partitions_size)
                j = 0;

            if (i / partitions_size == 0)
                partition_a[j] = element;
            if (i / partitions_size == 1)
                partition_b[j] = element;
            if (i / partitions_size == 2)
                partition_c[j] = element;
            if (i / partitions_size == 3)
                partition_d[j] = element;
            if (i / partitions_size >= 4)
                partition_e[j] = element;

            i++;
            j++;
        }

        Thread reverser_a = new Reverser(partition_a, attributes, htmlAttributes, htmlElements.length);
        Thread reverser_b = new Reverser(partition_b, attributes, htmlAttributes, htmlElements.length);
        Thread reverser_c = new Reverser(partition_c, attributes, htmlAttributes, htmlElements.length);
        Thread reverser_d = new Reverser(partition_d, attributes, htmlAttributes, htmlElements.length);
        Thread reverser_e = new Reverser(partition_e, attributes, htmlAttributes, htmlElements.length);

        reverser_a.start();
        reverser_b.start();
        reverser_c.start();
        reverser_d.start();
        reverser_e.start();

        try {
            reverser_a.join();
            reverser_b.join();
            reverser_c.join();
            reverser_d.join();
            reverser_e.join();
        } catch (InterruptedException e1) {
            Debug.printError("\nERROR: unable to terminate a thread");
        }

        Starter.setAllowedElements(Reverser.getAllowedElements());
    } else {
        reverse(injection);
    }

}

From source file:com.horizondigital.delta.UpdateService.java

private boolean zipadjust(String filenameIn, String filenameOut, long start, long currentOut, long totalOut) {
    Logger.d("zipadjust [%s] --> [%s]", filenameIn, filenameOut);

    // checking filesizes in the background as progress, because these
    // native functions don't have callbacks (yet) to do this

    (new File(filenameOut)).delete();

    Thread progress = getThreadedProgress(filenameOut, (new File(filenameIn)).getName(), start, currentOut,
            totalOut);//from ww  w  . j av a  2  s . c om
    progress.start();

    int ok = Native.zipadjust(filenameIn, filenameOut, 1);

    progress.interrupt();
    try {
        progress.join();
    } catch (InterruptedException e) {
        // We got interrupted in a very short wait, surprising, but not a problem. 'progress' will quit by itself.
        Logger.ex(e);
    }

    Logger.d("zipadjust --> %d", ok);

    return (ok == 1);
}

From source file:eu.chainfire.opendelta.UpdateService.java

private boolean zipadjust(String filenameIn, String filenameOut, long start, long currentOut, long totalOut) {
    Logger.d("zipadjust [%s] --> [%s]", filenameIn, filenameOut);

    // checking filesizes in the background as progress, because these
    // native functions don't have callbacks (yet) to do this

    (new File(filenameOut)).delete();

    Thread progress = getThreadedProgress(filenameOut, (new File(filenameIn)).getName(), start, currentOut,
            totalOut);/*from www  . ja  va 2 s .co  m*/
    progress.start();

    int ok = Native.zipadjust(filenameIn, filenameOut, 1);

    progress.interrupt();
    try {
        progress.join();
    } catch (InterruptedException e) {
        // We got interrupted in a very short wait, surprising, but not a
        // problem. 'progress' will quit by itself.
        Logger.ex(e);
    }

    Logger.d("zipadjust --> %d", ok);

    return (ok == 1);
}

From source file:com.thoughtworks.go.config.GoFileConfigDataSourceIntegrationTest.java

@Test
public void shouldBeAbleToConcurrentAccess() throws Exception {
    final String xml = ConfigMigrator.migrate(ConfigFileFixture.CONFIG_WITH_NANT_AND_EXEC_BUILDER);

    final List<Exception> errors = new Vector<>();
    Thread thread1 = new Thread(() -> {
        for (int i = 0; i < 5; i++) {
            try {
                goConfigDao.updateMailHost(new MailHost("hostname", 9999, "user", "password", false, false,
                        "from@local", "admin@local"));
            } catch (Exception e) {
                e.printStackTrace();//from  w w w  . j  av a 2 s.  co  m
                errors.add(e);
            }
        }
    }, "Update-license");

    Thread thread2 = new Thread(() -> {
        for (int i = 0; i < 5; i++) {
            try {
                dataSource.write(xml, false);
            } catch (Exception e) {
                e.printStackTrace();
                errors.add(e);
            }
        }
    }, "Modify-config");

    thread1.start();
    thread2.start();

    thread1.join();
    thread2.join();
    assertThat(errors.size(), is(0));
}

From source file:it.unimi.dsi.webgraph.algo.HyperBall.java

public void close() throws IOException {
    if (closed)// w  ww  .j a v a  2  s . c  om
        return;
    closed = true;

    lock.lock();
    try {
        completed = true;
        start.signalAll();
    } finally {
        lock.unlock();
    }

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

    if (external) {
        randomAccessFile.close();
        fileChannel.close();
        updateFile.delete();
    }
}

From source file:ext.services.network.TestNetworkUtils.java

/**
 * Test get connection url proxy with proxy.
 * // www .j a v  a  2  s .com
 *
 * @throws Exception the exception
 */
public void testGetConnectionURLProxyWithProxy() throws Exception {
    final ServerSocket socket = new ServerSocket(PROXY_PORT);
    Thread thread = new Thread("ProxySocketAcceptThread") {
        @Override
        public void run() {
            try {
                while (!bStop) {
                    Socket sock = socket.accept();
                    Log.debug("Accepted connection, sending back garbage and close socket...");
                    sock.getOutputStream().write(1);
                    sock.close();
                }
            } catch (IOException e) {
                Log.error(e);
            }
        }
    };
    thread.setDaemon(true); // to finish tests even if this is still running
    thread.start();
    Log.debug("Using local port: " + socket.getLocalPort());
    try {
        // useful content when inet access is allowed
        Conf.setProperty(Const.CONF_NETWORK_NONE_INTERNET_ACCESS, "false");
        HttpURLConnection connection = NetworkUtils.getConnection(new java.net.URL(URL),
                new Proxy(Type.SOCKS, "localhost", socket.getLocalPort(), "user", "password"));
        assertNotNull(connection);
        connection.disconnect();
    } finally {
        bStop = true;
        socket.close();
        thread.join();
    }
}

From source file:com.horizondigital.delta.UpdateService.java

private boolean dedelta(String filenameSource, String filenameDelta, String filenameOut, long start,
        long currentOut, long totalOut) {
    Logger.d("dedelta [%s] --> [%s] --> [%s]", filenameSource, filenameDelta, filenameOut);

    // checking filesizes in the background as progress, because these
    // native functions don't have callbacks (yet) to do this

    (new File(filenameOut)).delete();

    Thread progress = getThreadedProgress(filenameOut, (new File(filenameDelta)).getName(), start, currentOut,
            totalOut);//w w w  .  java 2s .  co  m
    progress.start();

    int ok = Native.dedelta(filenameSource, filenameDelta, filenameOut);

    progress.interrupt();
    try {
        progress.join();
    } catch (InterruptedException e) {
        // We got interrupted in a very short wait, surprising, but not a problem. 'progress' will quit by itself.
        Logger.ex(e);
    }

    Logger.d("dedelta --> %d", ok);

    return (ok == 1);
}

From source file:eu.chainfire.opendelta.UpdateService.java

private boolean dedelta(String filenameSource, String filenameDelta, String filenameOut, long start,
        long currentOut, long totalOut) {
    Logger.d("dedelta [%s] --> [%s] --> [%s]", filenameSource, filenameDelta, filenameOut);

    // checking filesizes in the background as progress, because these
    // native functions don't have callbacks (yet) to do this

    (new File(filenameOut)).delete();

    Thread progress = getThreadedProgress(filenameOut, (new File(filenameDelta)).getName(), start, currentOut,
            totalOut);//from   ww  w .j ava  2s  . c o  m
    progress.start();

    int ok = Native.dedelta(filenameSource, filenameDelta, filenameOut);

    progress.interrupt();
    try {
        progress.join();
    } catch (InterruptedException e) {
        // We got interrupted in a very short wait, surprising, but not a
        // problem. 'progress' will quit by itself.
        Logger.ex(e);
    }

    Logger.d("dedelta --> %d", ok);

    return (ok == 1);
}

From source file:org.ulyssis.ipp.reader.Reader.java

/**
 * Run the reader. Reader implements runnable, so that we can
 * do this in its own thread./*from  w  w  w.j  a va 2s  .c om*/
 */
@Override
public void run() {
    LOG.info("Spinning up reader!");
    ReaderConfig.Type type = Config.getCurrentConfig().getReader(options.getId()).getType();
    if (type == ReaderConfig.Type.LLRP) {
        initSpeedway();
        if (!speedwayInitialized) {
            shutdownHook();
            return;
        }
    } else if (type == ReaderConfig.Type.SIMULATOR) {
        initSimulator();
    }
    Thread commandThread = new Thread(commandProcessor);
    commandThread.start();
    statusReporter.broadcast(new StatusMessage(StatusMessage.MessageType.STARTED_UP,
            String.format("Started up reader %s!", options.getId())));
    try {
        while (!Thread.currentThread().isInterrupted()) {
            Duration maxUpdateInterval = Duration.ofMillis(Config.getCurrentConfig().getMaxUpdateInterval());
            if (maxUpdateInterval.minus(Duration.between(lastUpdate, Instant.now())).isNegative()) {
                lastUpdate = Instant.now();
                LOG.warn("No update received in {} seconds!", maxUpdateInterval.getSeconds());
                statusReporter.broadcast(new StatusMessage(StatusMessage.MessageType.NO_UPDATES,
                        String.format("No update received in %s seconds!", maxUpdateInterval.getSeconds())));
            }
            Thread.sleep(1000L);
        }
    } catch (InterruptedException e) {
        // We don't care about this exception
    }
    commandProcessor.stop();
    commandThread.interrupt();
    try {
        commandThread.join();
    } catch (InterruptedException ignored) {
    }
    shutdownHook();
}

From source file:com.thoughtworks.go.server.service.ConfigSaveDeadlockDetectionIntegrationTest.java

@Test
public void shouldNotDeadlockWhenAllPossibleWaysOfUpdatingTheConfigAreBeingUsedAtTheSameTime()
        throws Exception {
    int EXISTING_ENV_COUNT = goConfigService.cruiseConfig().getEnvironments().size();
    final ArrayList<Thread> group1 = new ArrayList<>();
    final ArrayList<Thread> group2 = new ArrayList<>();
    final ArrayList<Thread> group3 = new ArrayList<>();
    final ArrayList<Thread> group4 = new ArrayList<>();
    final ArrayList<Thread> group5 = new ArrayList<>();
    int count = 100;
    final int pipelineCreatedThroughApiCount = count;
    final int pipelineCreatedThroughUICount = count;
    final int configRepoAdditionThreadCount = count;
    final int configRepoDeletionThreadCount = count;
    final int fullConfigSaveThreadCount = count;

    for (int i = 0; i < pipelineCreatedThroughUICount; i++) {
        Thread thread = configSaveThread(i);
        group1.add(thread);//from   ww  w  .  j  av  a  2  s  . c  o m
    }

    for (int i = 0; i < pipelineCreatedThroughApiCount; i++) {
        Thread thread = pipelineSaveThread(i);
        group2.add(thread);
    }

    ConfigReposConfig configRepos = new ConfigReposConfig();
    for (int i = 0; i < configRepoAdditionThreadCount; i++) {
        ConfigRepoConfig configRepoConfig = new ConfigRepoConfig(git("url" + i), "plugin");
        configRepos.add(configRepoConfig);
        Thread thread = configRepoSaveThread(configRepoConfig, i);
        group3.add(thread);
    }

    for (int i = 0; i < configRepoDeletionThreadCount; i++) {
        ConfigRepoConfig configRepoConfig = new ConfigRepoConfig(git("to-be-deleted-url" + i), "plugin");
        cachedGoPartials.addOrUpdate(configRepoConfig.getMaterialConfig().getFingerprint(), PartialConfigMother
                .withPipeline("to-be-deleted" + i, new RepoConfigOrigin(configRepoConfig, "plugin")));
        configRepos.add(configRepoConfig);
        Thread thread = configRepoDeleteThread(configRepoConfig, i);
        group4.add(thread);
    }
    for (int i = 0; i < fullConfigSaveThreadCount; i++) {
        Thread thread = fullConfigSaveThread(i);
        group5.add(thread);
    }
    configHelper.setConfigRepos(configRepos);
    for (int i = 0; i < count; i++) {
        Thread timerThread = null;
        try {
            timerThread = createThread(new Runnable() {
                @Override
                public void run() {
                    try {
                        writeConfigToFile(new File(goConfigDao.fileLocation()));
                    } catch (Exception e) {
                        e.printStackTrace();
                        fail("Failed with error: " + e.getMessage());
                    }
                    cachedGoConfig.forceReload();
                }
            }, "timer-thread");
        } catch (InterruptedException e) {
            fail(e.getMessage());
        }

        try {
            group1.get(i).start();
            group2.get(i).start();
            group3.get(i).start();
            group4.get(i).start();
            group5.get(i).start();
            timerThread.start();
            group1.get(i).join();
            group2.get(i).join();
            group3.get(i).join();
            group4.get(i).join();
            group5.get(i).join();
            timerThread.join();
        } catch (InterruptedException e) {
            fail(e.getMessage());
        }

    }

    assertThat(goConfigService.getAllPipelineConfigs().size(),
            is(pipelineCreatedThroughApiCount + pipelineCreatedThroughUICount + configRepoAdditionThreadCount));
    assertThat(goConfigService.getConfigForEditing().getAllPipelineConfigs().size(),
            is(pipelineCreatedThroughApiCount + pipelineCreatedThroughUICount));
    assertThat(goConfigService.getConfigForEditing().getEnvironments().size(),
            is(fullConfigSaveThreadCount + EXISTING_ENV_COUNT));
}