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:eu.stratosphere.pact.runtime.task.MatchTaskTest.java

@Test
public void testHashSecondCancelMatchTaskWhileMatching() {

    int keyCnt = 20;
    int valCnt = 20;

    super.initEnvironment(6 * 1024 * 1024);
    super.addInput(new UniformPactRecordGenerator(keyCnt, valCnt, false), 1);
    super.addInput(new UniformPactRecordGenerator(keyCnt, valCnt, false), 2);
    super.addOutput(new NirvanaOutputList());

    final MatchTask<PactRecord, PactRecord, PactRecord> testTask = new MatchTask<PactRecord, PactRecord, PactRecord>();
    super.getTaskConfig().setLocalStrategy(LocalStrategy.HYBRIDHASH_SECOND);
    super.getTaskConfig().setMemorySize(6 * 1024 * 1024);
    super.getTaskConfig().setNumFilehandles(4);

    final int[] keyPos1 = new int[] { 0 };
    final int[] keyPos2 = new int[] { 0 };
    @SuppressWarnings("unchecked")
    final Class<? extends Key>[] keyClasses = (Class<? extends Key>[]) new Class[] { PactInteger.class };

    PactRecordComparatorFactory.writeComparatorSetupToConfig(super.getTaskConfig().getConfiguration(),
            super.getTaskConfig().getPrefixForInputParameters(0), keyPos1, keyClasses);
    PactRecordComparatorFactory.writeComparatorSetupToConfig(super.getTaskConfig().getConfiguration(),
            super.getTaskConfig().getPrefixForInputParameters(1), keyPos2, keyClasses);

    super.registerTask(testTask, MockDelayingMatchStub.class);

    Thread taskRunner = new Thread() {
        @Override/*  ww  w .  j  av  a 2  s  .  com*/
        public void run() {
            try {
                testTask.invoke();
            } catch (Exception ie) {
                ie.printStackTrace();
                Assert.fail("Task threw exception although it was properly canceled");
            }
        }
    };
    taskRunner.start();

    TaskCancelThread tct = new TaskCancelThread(1, taskRunner, testTask);
    tct.start();

    try {
        tct.join();
        taskRunner.join();
    } catch (InterruptedException ie) {
        Assert.fail("Joining threads failed");
    }

}

From source file:com.trifork.riak.RiakClient.java

public ByteString[] store(RiakObject[] values, RequestMeta meta) throws IOException {

    RiakConnection c = getConnection();//from w  w w  .  j  av a 2 s.  com
    try {
        BulkReader reader = new BulkReader(c, values.length);
        Thread worker = new Thread(reader);
        worker.start();

        DataOutputStream dout = c.getOutputStream();

        for (int i = 0; i < values.length; i++) {
            RiakObject value = values[i];

            RPB.RpbPutReq.Builder builder = RPB.RpbPutReq.newBuilder().setBucket(value.getBucketBS())
                    .setKey(value.getKeyBS()).setContent(value.buildContent());

            if (value.getVclock() != null) {
                builder.setVclock(value.getVclock());
            }

            builder.setReturnBody(false);

            if (meta != null) {

                if (meta.writeQuorum != null) {
                    builder.setW(meta.writeQuorum.intValue());
                }

                if (meta.durableWriteQuorum != null) {
                    builder.setDw(meta.durableWriteQuorum.intValue());
                }
            }

            RpbPutReq req = builder.build();

            int len = req.getSerializedSize();
            dout.writeInt(len + 1);
            dout.write(MSG_PutReq);
            req.writeTo(dout);
        }

        dout.flush();

        try {
            worker.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return reader.vclocks;
    } finally {
        release(c);
    }
}

From source file:net.sourceforge.fenixedu.domain.accounting.report.events.EventReportQueueJob.java

private SheetData<EventBean> allEvents(final StringBuilder errors) {

    List<String> allEventsExternalIds = getAllEventsExternalIds();
    logger.info(String.format("%s events to process", allEventsExternalIds.size()));

    Integer blockRead = 0;/*w w  w .java  2 s .  co  m*/

    final List<EventBean> result = Collections.synchronizedList(new ArrayList<EventBean>());

    while (blockRead < allEventsExternalIds.size()) {
        Integer inc = BLOCK;

        if (blockRead + inc >= allEventsExternalIds.size()) {
            inc = allEventsExternalIds.size() - blockRead;
        }

        final List<String> block = allEventsExternalIds.subList(blockRead, blockRead + inc);
        blockRead += inc;

        Thread thread = new Thread() {

            @Override
            @Atomic(mode = TxMode.READ)
            public void run() {
                for (String oid : block) {
                    Event event = null;
                    try {
                        event = FenixFramework.getDomainObject(oid);
                        if (!isAccountingEventForReport(event)) {
                            continue;
                        }
                        result.add(writeEvent(event));
                    } catch (Throwable e) {
                        errors.append(getErrorLine(event, e));
                    }
                }
            }
        };

        thread.start();

        try {
            thread.join();
        } catch (InterruptedException e) {
        }

        logger.info(String.format("Read %s events", blockRead));
    }

    logger.info(String.format("Catch %s events ", result.size()));

    return new SheetData<EventBean>(result) {

        @Override
        protected void makeLine(EventBean bean) {
            addCell("Identificador", bean.externalId);
            addCell("Aluno", bean.studentNumber);
            addCell("Nome", bean.studentName);
            addCell("Email", bean.email);
            addCell("Data inscrio", bean.registrationStartDate);
            addCell("Ano lectivo", bean.executionYear);
            addCell("Tipo de matricula", bean.studiesType);
            addCell("Nome do Curso", bean.degreeName);
            addCell("Tipo de curso", bean.degreeType);
            addCell("Programa doutoral", bean.phdProgramName);
            addCell("ECTS inscritos", bean.enrolledECTS);
            addCell("Regime", bean.regime);
            addCell("Modelo de inscrio", bean.enrolmentModel);
            addCell("Residncia - Ano", bean.residenceYear);
            addCell("Residncia - Ms", bean.residenceMonth);
            addCell("Tipo de divida", bean.description);
            addCell("Data de criao", bean.whenOccured);
            addCell("Valor Total", bean.totalAmount);
            addCell("Valor Pago", bean.payedAmount);
            addCell("Valor em divida", bean.amountToPay);
            addCell("Valor Reembolsvel", bean.reimbursableAmount);
            addCell("Desconto", bean.totalDiscount);
            addCell("Dvida Associada", bean.relatedEvent);
            addCell("Id. Fiscal Entidade", bean.debtorFiscalId);
            addCell("Nome Entidade", bean.debtorName);

            List<InstallmentWrapper> list = bean.installments;

            if (list != null) {
                for (InstallmentWrapper installment : list) {
                    addCell(installment.getExpirationDateLabel(), installment.getExpirationDate());
                    addCell(installment.getAmountToPayLabel(), installment.getAmountToPay());
                    addCell(installment.getRemainingAmountLabel(), installment.getRemainingAmount());
                }
            }
        }

    };
}

From source file:com.tesora.dve.sql.logfile.LogFileTest.java

protected void configMTTest(final TestName tn, BackingSchema leftType, BackingSchema rightType)
        throws Throwable {
    aborted = false;/*  ww w  .  ja  va2 s  .c o  m*/
    // first off, mt tests cannot run if both backing types are pe
    int npet = 0;
    if ((leftType != null) && (leftType != BackingSchema.NATIVE)) {
        npet++;
    }
    if ((rightType != null) && (rightType != BackingSchema.NATIVE)) {
        npet++;
    }
    if (npet > 1) {
        throw new Throwable("Misconfiguration of configMTTest.  Only one side is allowed to be PE");
    }

    lastRan = new LastRunConfig(tn, leftType, rightType);

    // mt single, native mt single mirror, mt multi, native mt multi mirror

    final TestResource firstResource = getTestResource(tn, leftType);
    final TestResource secondResource = getTestResource(tn, rightType);
    try {

        TestResource rootResource = null;
        TestResource nativeResource = null;

        if (secondResource == null) {
            // either mt single or mt multi - in this case we don't need to worry about setting up the native connection
            rootResource = firstResource;
        } else if (firstResource.getDDL() != getNativeDDL()) {
            throw new Throwable(
                    "Misconfiguration of configMTTest.  Mirror test configured but lhs is not native");
        } else {
            nativeResource = firstResource;
            rootResource = secondResource;
        }

        if (useThreads(tn) && (nativeResource != null)) {
            throw new Throwable(
                    "Misconveriguration of configMTTest.  Thread use requested but native resource present");
        }

        // first up, remove any existing mt user
        removeUser(rootResource.getConnection(), mtUserName, mtUserAccess);
        // now put the system in mt mode
        // now we're going to load the database.  note that this is done as the root user.
        if (mtLoadDatabase()) {
            preTest(tn, nativeResource, rootResource, getFile(tn, 0), 1);
        }
        // now we're going to run the actual test for the root user
        MultitenantMode mm = null;
        if (rootResource.getDDL() instanceof PEDDL) {
            final PEDDL peddl = (PEDDL) rootResource.getDDL();
            mm = peddl.getMultitenantMode();
        }
        if (!skipInitialMTLoad(tn)) {
            if (nativeResource != null) {
                runTest(tn, nativeResource, rootResource, getFile(tn, 0), createSchema(tn), verifyUpdates(tn),
                        getSkipStatements(tn, 0), getBreakpoints(tn), ignoreResults(tn), compareUnordered(tn),
                        verifyTempTables(tn), TestPart.BODY);
            } else {
                runTest(tn, rootResource, null, getFile(tn, 0), createSchema(tn), verifyUpdates(tn),
                        getSkipStatements(tn, 0), getBreakpoints(tn), ignoreResults(tn), compareUnordered(tn),
                        verifyTempTables(tn), TestPart.BODY);
            }
        } else if (createSchema(tn)) {
            if (nativeResource != null) {
                runDDL(true, nativeResource);
            }
            runDDL(true, rootResource);
        }

        maybeConnect(rootResource);

        // create the second user - have to delay because we don't yet add users upon new site
        rootResource.getConnection().execute(
                "create user '" + mtUserName + "'@'" + mtUserAccess + "' identified by '" + mtUserName + "'");

        if (useThreads(tn)) {
            final LogFileTest me = this;
            final ArrayList<Thread> threads = new ArrayList<Thread>();
            for (int i = 0; i < maxTenants(tn); i++) {
                final FileResource log = getFile(tn, i);
                if (log != null) {
                    System.out.println("MT tenant " + i + " running log " + log.getFileName());
                }

                // set perms
                final String ctenant = tenantKernel + i;
                rootResource.getConnection()
                        .execute("create tenant " + ctenant + " '" + tn.toString() + " " + ctenant + "'");
                rootResource.getConnection().execute("grant all on " + ctenant + ".* to '" + mtUserName + "'@'"
                        + mtUserAccess + "' identified by '" + mtUserName + "'");

                final long[] skips = getSkipStatements(tn, i);

                final TestResource tenantResource = new TestResource(
                        new ProxyConnectionResource(mtUserName, mtUserName),
                        getSingleSiteDDL().buildTenantDDL(ctenant));

                if (shouldCreateUserForTenant()) {
                    createUserForTenant(rootResource, ctenant, ctenant);
                }

                // dup our resources
                threads.add(new Thread(ctenant) {
                    @Override
                    public void run() {
                        try {
                            if (!mtLoadDatabase()) {
                                // if we didn't run the pretest earlier then
                                // we want to do it for each tenant
                                preTest(tn, tenantResource, null, log, 1);
                            }
                            runTest(tn, tenantResource, null, log, false, verifyUpdates(tn), skips,
                                    getBreakpoints(tn), ignoreResults(tn), compareUnordered(tn),
                                    verifyTempTables(tn), TestPart.BODY);
                        } catch (final Throwable t) {
                            if ("Aborting log file run".equals(t.getMessage())) {
                                // don't overreport, just get out
                                return;
                            }
                            System.err.println("Tenant: " + ctenant + " FAILED");
                            t.printStackTrace();
                            me.aborted = true;
                            fail(t.getMessage());
                            throw new RuntimeException(t);
                        } finally {
                            maybeDisconnect(tenantResource);
                        }
                    }
                });
            }
            for (final Thread t : threads) {
                t.start();
            }
            while (!threads.isEmpty()) {
                for (final Iterator<Thread> iter = threads.iterator(); iter.hasNext();) {
                    final Thread thr = iter.next();
                    try {
                        thr.join();
                        iter.remove();
                    } catch (final InterruptedException ie) {
                        // ignore
                    }
                }
            }
        } else {

            for (int i = 0; i < maxTenants(tn); i++) {

                final FileResource log = getFile(tn, i);
                if (log != null) {
                    System.out.println("MT tenant " + i + " running log " + log.getFileName());
                }

                // make sure everything is connected
                maybeConnect(nativeResource);
                maybeConnect(rootResource);

                if (nativeResource != null) {
                    nativeResource.getConnection()
                            .execute("drop database if exists " + nativeResource.getDDL().getDatabaseName());
                    nativeResource.getConnection()
                            .execute(nativeResource.getDDL().getCreateDatabaseStatement());
                }
                final String ctenant = tenantKernel + i;
                rootResource.getConnection()
                        .execute("create tenant " + ctenant + " '" + tn.toString() + " " + ctenant + "'");
                rootResource.getConnection().execute("grant all on " + ctenant + ".* to '" + mtUserName + "'@'"
                        + mtUserAccess + "' identified by '" + mtUserName + "'");
                final TestResource tenantResource = new TestResource(
                        new ProxyConnectionResource(mtUserName, mtUserName),
                        getSingleSiteDDL().buildTenantDDL(ctenant));
                // the tenant does not need to create schema, but if we're doing native we just tossed the backing database.
                // add that in now.
                if (nativeResource != null) {
                    preTest(tn, nativeResource, tenantResource, log, i + 1);
                }
                // now run again
                if (nativeResource != null) {
                    runTest(tn, nativeResource, tenantResource, log, false, verifyUpdates(tn),
                            getSkipStatements(tn, i), getBreakpoints(tn), ignoreResults(tn),
                            compareUnordered(tn), verifyTempTables(tn), TestPart.BODY);
                } else {
                    runTest(tn, tenantResource, null, log, false, verifyUpdates(tn), getSkipStatements(tn, i),
                            getBreakpoints(tn), ignoreResults(tn), compareUnordered(tn), verifyTempTables(tn),
                            TestPart.BODY);
                }

                maybeDisconnect(nativeResource);
                maybeDisconnect(rootResource);
                maybeDisconnect(tenantResource);
            }
        }

        // report on private/public tables
        if (getNoisyInterval() != null) {
            try {
                System.out.println("Sleeping for 2 seconds to collect adaptive garbage");
                Thread.sleep(2000);
            } catch (final InterruptedException ie) {
                // ignore
            }
            try (DBHelperConnectionResource dbh = new DBHelperConnectionResource()) {
                dbh.execute("use " + PEConstants.CATALOG);
                System.out.println("Multitenant mode: " + (mm != null ? mm.getPersistentValue() : "null MM"));
                System.out.println("number of table scopes:");
                System.out.println(dbh.printResults("select count(*) from scope"));
                System.out.println("private tables:");
                //         System.out.println(dbh.printResults("select count(*) from user_table ut, user_database ud where ut.user_database_id = ud.user_database_id and ud.multitenant_mode != 'off' and ut.privtab_ten_id is not null"));
                System.out.println(dbh.printResults(
                        "select ut.name, s.local_name, t.ext_tenant_id from user_table ut, scope s, tenant t where ut.privtab_ten_id is not null and ut.table_id = s.scope_table_id and s.scope_tenant_id = t.tenant_id"));
                System.out.println("number of shared tables:");
                System.out.println(dbh.printResults(
                        "select count(*) from user_table ut, user_database ud where ut.user_database_id = ud.user_database_id and ud.multitenant_mode != 'off' and ut.privtab_ten_id is null"));
                //         System.out.println("Compression:");
                //         System.out.println(dbh.printResults("select coalesce(local_name,ut.name), count(distinct scope_table_id), count(scope_table_id) from scope, user_table ut where scope_table_id = ut.table_id group by local_name order by local_name"));
            }
        }
    } finally {
        maybeDisconnect(firstResource);
        maybeDisconnect(secondResource);
    }

    // allow someone to do something after the test run
    postTest(tn, firstResource, secondResource, getFile(tn, 0), 1);
}

From source file:com.datatorrent.stram.engine.GenericNodeTest.java

private void testCheckpointDistance(int dagCheckPoint, int opCheckPoint) throws InterruptedException {
    int windowWidth = 50;
    long sleeptime = 25L;
    int maxWindows = 60;
    // Adding some extra time for the windows to finish
    long maxSleep = windowWidth * maxWindows + 5000;

    ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(1, "default");
    final WindowGenerator windowGenerator = new WindowGenerator(executorService, 1024);
    windowGenerator.setWindowWidth(windowWidth);
    windowGenerator.setFirstWindow(executorService.getCurrentTimeMillis());
    windowGenerator.setCheckpointCount(dagCheckPoint, 0);
    //GenericOperator go = new GenericOperator();
    CheckpointDistanceOperator go = new CheckpointDistanceOperator();
    go.maxWindows = maxWindows;/*from  www.j a  v a2s  .  co  m*/

    List<Integer> checkpoints = new ArrayList<Integer>();

    int window = 0;
    while (window < maxWindows) {
        window = (int) Math.ceil((double) (window + 1) / dagCheckPoint) * dagCheckPoint;
        window = (int) Math.ceil((double) window / opCheckPoint) * opCheckPoint;
        checkpoints.add(window);
    }

    final StreamContext stcontext = new StreamContext("s1");
    DefaultAttributeMap attrMap = new DefaultAttributeMap();
    attrMap.put(Context.DAGContext.CHECKPOINT_WINDOW_COUNT, dagCheckPoint);
    attrMap.put(Context.OperatorContext.CHECKPOINT_WINDOW_COUNT, opCheckPoint);
    final OperatorContext context = new com.datatorrent.stram.engine.OperatorContext(0, attrMap, null);
    final GenericNode gn = new GenericNode(go, context);
    gn.setId(1);

    //DefaultReservoir reservoir1 = new DefaultReservoir("ip1Res", 1024);
    //DefaultReservoir reservoir2 = new DefaultReservoir("ip2Res", 1024);

    //gn.connectInputPort("ip1", reservoir1);
    //gn.connectInputPort("ip2", reservoir2);
    gn.connectInputPort("ip1", windowGenerator.acquireReservoir("ip1", 1024));
    gn.connectInputPort("ip2", windowGenerator.acquireReservoir("ip2", 1024));
    gn.connectOutputPort("op", Sink.BLACKHOLE);

    final AtomicBoolean ab = new AtomicBoolean(false);
    Thread t = new Thread() {
        @Override
        public void run() {
            gn.setup(context);
            windowGenerator.activate(stcontext);
            gn.activate();
            ab.set(true);
            gn.run();
            windowGenerator.deactivate();
            gn.deactivate();
            gn.teardown();
        }
    };
    t.start();

    long interval = 0;
    do {
        Thread.sleep(sleeptime);
        interval += sleeptime;
    } while ((go.numWindows < maxWindows) && (interval < maxSleep));

    Assert.assertEquals("Number distances", maxWindows, go.numWindows);
    int chkindex = 0;
    int nextCheckpoint = checkpoints.get(chkindex++);
    for (int i = 0; i < maxWindows; ++i) {
        if ((i + 1) > nextCheckpoint) {
            nextCheckpoint = checkpoints.get(chkindex++);
        }
        Assert.assertEquals("Windows from checkpoint for " + i, nextCheckpoint - i, (int) go.distances.get(i));
    }

    gn.shutdown();
    t.join();
}

From source file:hr.fer.zemris.vhdllab.platform.ui.command.DevFloodWithCompliationRequestsCommand.java

private void floodWithCompilationRequests(final Integer fileId, final String name) {
    String input = JOptionPane.showInputDialog("How many?", "30");
    int floodCount = Integer.parseInt(input);

    final List<String> results = Collections.synchronizedList(new ArrayList<String>(floodCount));
    final CyclicBarrier barrier = new CyclicBarrier(floodCount);
    List<Thread> threads = new ArrayList<Thread>(floodCount);
    long start = System.currentTimeMillis();
    for (int i = 0; i < floodCount; i++) {
        Thread thread = new Thread(new Runnable() {
            @SuppressWarnings("synthetic-access")
            @Override/*from w  ww.  j av  a  2s . c  om*/
            public void run() {
                try {
                    barrier.await();
                } catch (Exception e) {
                    throw new UnhandledException(e);
                }
                logger.info("sending at: " + System.currentTimeMillis());
                List<CompilationMessage> messages;
                try {
                    messages = simulator.compile(fileId);
                } catch (SimulatorTimeoutException e) {
                    String message = localizationSource.getMessage("simulator.compile.timout",
                            new Object[] { name });
                    messages = Collections.singletonList(new CompilationMessage(message));
                } catch (NoAvailableProcessException e) {
                    String message = localizationSource.getMessage("simulator.compile.no.processes",
                            new Object[] { name });
                    messages = Collections.singletonList(new CompilationMessage(message));
                }

                if (messages.isEmpty()) {
                    results.add("Successful");
                } else {
                    results.add(messages.get(0).getText());
                }
            }
        });
        thread.start();
        threads.add(thread);
    }

    logger.info("waiting for threads to complete...");
    for (Thread thread : threads) {
        try {
            thread.join();
        } catch (InterruptedException e) {
            throw new UnhandledException(e);
        }
    }
    long end = System.currentTimeMillis();
    logger.info("ended in " + (end - start) + " ms");
    showResults(results);
}