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:juicebox.data.MatrixZoomData.java

/**
 * Return the blocks of normalized, observed values overlapping the rectangular region specified.
 * The units are "bins"/*w w  w. j ava 2  s.c om*/
 *
 * @param binY1 leftmost position in "bins"
 * @param binX2 rightmost position in "bins"
 * @param binY2 bottom position in "bins"
 * @param no    normalization type
 * @return List of overlapping blocks, normalized
 */
public List<Block> getNormalizedBlocksOverlapping(int binX1, int binY1, int binX2, int binY2,
        final NormalizationType no) {

    int col1 = binX1 / blockBinCount;
    int row1 = binY1 / blockBinCount;

    int col2 = binX2 / blockBinCount;
    int row2 = binY2 / blockBinCount;

    int maxSize = (col2 - col1 + 1) * (row2 - row1 + 1);

    final List<Block> blockList = new ArrayList<Block>(maxSize);
    final List<Integer> blocksToLoad = new ArrayList<Integer>();
    for (int r = row1; r <= row2; r++) {
        for (int c = col1; c <= col2; c++) {
            int blockNumber = r * getBlockColumnCount() + c;

            String key = getKey() + "_" + blockNumber + "_" + no;
            Block b;
            if (HiCGlobals.useCache && blockCache.containsKey(key)) {
                b = blockCache.get(key);
                blockList.add(b);
            } else {
                blocksToLoad.add(blockNumber);
            }
        }
    }

    final AtomicInteger errorCounter = new AtomicInteger();

    List<Thread> threads = new ArrayList<Thread>();
    for (final int blockNumber : blocksToLoad) {
        Runnable loader = new Runnable() {
            @Override
            public void run() {
                try {
                    String key = getKey() + "_" + blockNumber + "_" + no;
                    Block b = reader.readNormalizedBlock(blockNumber, MatrixZoomData.this, no);
                    if (b == null) {
                        b = new Block(blockNumber); // An empty block
                    }
                    if (HiCGlobals.useCache) {
                        blockCache.put(key, b);
                    }
                    blockList.add(b);
                } catch (IOException e) {
                    errorCounter.incrementAndGet();
                }
            }
        };

        Thread t = new Thread(loader);
        threads.add(t);
        t.start();
    }

    // Wait for all threads to complete
    for (Thread t : threads) {
        try {
            t.join();
        } catch (InterruptedException ignore) {
        }
    }

    // untested since files got fixed - MSS
    if (errorCounter.get() > 0) {
        return null;
    }

    return blockList;
}

From source file:eu.stratosphere.pact.runtime.task.CoGroupTaskTest.java

@Test
public void testCancelCoGroupTaskWhileCoGrouping() {
    int keyCnt = 100;
    int valCnt = 5;

    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 CoGroupTask<PactRecord, PactRecord, PactRecord> testTask = new CoGroupTask<PactRecord, PactRecord, PactRecord>();
    super.getTaskConfig().setLocalStrategy(LocalStrategy.SORT_BOTH_MERGE);
    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, MockDelayingCoGroupStub.class);

    Thread taskRunner = new Thread() {
        @Override/*from   www  .j  a  v  a2s.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(2, taskRunner, testTask);
    tct.start();

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

From source file:eu.stratosphere.pact.runtime.task.CrossTaskTest.java

@Test
public void testCancelBlockCrossTaskInit() {

    int keyCnt = 10;
    int valCnt = 1;

    super.initEnvironment(1 * 1024 * 1024);
    super.addInput(new UniformPactRecordGenerator(keyCnt, valCnt, false), 1);
    super.addInput(new DelayingInfinitiveInputIterator(100), 2);
    super.addOutput(this.outList);

    final CrossTask<PactRecord, PactRecord, PactRecord> testTask = new CrossTask<PactRecord, PactRecord, PactRecord>();
    super.getTaskConfig().setLocalStrategy(LocalStrategy.NESTEDLOOP_BLOCKED_OUTER_FIRST);
    super.getTaskConfig().setMemorySize(1 * 1024 * 1024);

    super.registerTask(testTask, MockCrossStub.class);

    Thread taskRunner = new Thread() {
        @Override/*  w  w  w  .ja  v a2  s  . c  om*/
        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:eu.stratosphere.pact.runtime.task.CrossTaskTest.java

@Test
public void testCancelBlockCrossTaskCrossing() {

    int keyCnt = 10;
    int valCnt = 1;

    super.initEnvironment(1 * 1024 * 1024);
    super.addInput(new UniformPactRecordGenerator(keyCnt, valCnt, false), 1);
    super.addInput(new DelayingInfinitiveInputIterator(100), 2);
    super.addOutput(this.outList);

    final CrossTask<PactRecord, PactRecord, PactRecord> testTask = new CrossTask<PactRecord, PactRecord, PactRecord>();
    super.getTaskConfig().setLocalStrategy(LocalStrategy.NESTEDLOOP_BLOCKED_OUTER_SECOND);
    super.getTaskConfig().setMemorySize(1 * 1024 * 1024);

    super.registerTask(testTask, MockCrossStub.class);

    Thread taskRunner = new Thread() {
        @Override//from   ww  w.j  av  a  2 s. c o  m
        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:eu.stratosphere.pact.runtime.task.CrossTaskTest.java

@Test
public void testCancelStreamCrossTaskInit() {

    int keyCnt = 10;
    int valCnt = 1;

    super.initEnvironment(1 * 1024 * 1024);
    super.addInput(new UniformPactRecordGenerator(keyCnt, valCnt, false), 1);
    super.addInput(new DelayingInfinitiveInputIterator(100), 2);
    super.addOutput(this.outList);

    final CrossTask<PactRecord, PactRecord, PactRecord> testTask = new CrossTask<PactRecord, PactRecord, PactRecord>();
    super.getTaskConfig().setLocalStrategy(LocalStrategy.NESTEDLOOP_STREAMED_OUTER_FIRST);
    super.getTaskConfig().setMemorySize(1 * 1024 * 1024);

    super.registerTask(testTask, MockCrossStub.class);

    Thread taskRunner = new Thread() {
        @Override/*from   w w  w.j  av  a  2 s.c  om*/
        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:eu.stratosphere.pact.runtime.task.CrossTaskTest.java

@Test
public void testCancelStreamCrossTaskCrossing() {

    int keyCnt = 10;
    int valCnt = 1;

    super.initEnvironment(1 * 1024 * 1024);
    super.addInput(new UniformPactRecordGenerator(keyCnt, valCnt, false), 1);
    super.addInput(new DelayingInfinitiveInputIterator(100), 2);
    super.addOutput(this.outList);

    final CrossTask<PactRecord, PactRecord, PactRecord> testTask = new CrossTask<PactRecord, PactRecord, PactRecord>();
    super.getTaskConfig().setLocalStrategy(LocalStrategy.NESTEDLOOP_STREAMED_OUTER_SECOND);
    super.getTaskConfig().setMemorySize(1 * 1024 * 1024);

    super.registerTask(testTask, MockCrossStub.class);

    Thread taskRunner = new Thread() {
        @Override/*from   w  ww .j av a 2  s.  c  om*/
        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:eu.stratosphere.pact.runtime.task.CoGroupTaskTest.java

@Test
public void testCancelCoGroupTaskWhileSorting1() {

    int keyCnt = 10;
    int valCnt = 2;

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

    final CoGroupTask<PactRecord, PactRecord, PactRecord> testTask = new CoGroupTask<PactRecord, PactRecord, PactRecord>();
    super.getTaskConfig().setLocalStrategy(LocalStrategy.SORT_BOTH_MERGE);
    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, MockCoGroupStub.class);

    Thread taskRunner = new Thread() {
        @Override/*ww  w. ja v  a 2  s  . c o m*/
        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:eu.stratosphere.pact.runtime.task.CoGroupTaskTest.java

@Test
public void testCancelCoGroupTaskWhileSorting2() {

    int keyCnt = 10;
    int valCnt = 2;

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

    final CoGroupTask<PactRecord, PactRecord, PactRecord> testTask = new CoGroupTask<PactRecord, PactRecord, PactRecord>();
    super.getTaskConfig().setLocalStrategy(LocalStrategy.SORT_BOTH_MERGE);
    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, MockCoGroupStub.class);

    Thread taskRunner = new Thread() {
        @Override/*w w  w  . j a  va2 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.amalto.core.load.LoadParserTest.java

public void test3MultiThread() {
    int threadNumber = 20;
    Set<Thread> threads = new HashSet<Thread>(threadNumber + 1);

    Runnable runnable = new Runnable() {
        public void run() {
            for (int i = 0; i < 10; i++) {
                InputStream testResource = this.getClass().getResourceAsStream("test3.xml");
                assertNotNull(testResource);

                ParserTestCallback callback = new ParserTestCallback();

                LoadParser.Configuration config = new LoadParser.Configuration("Product", new String[] { "Id" },
                        false, "clusterName", "modelName", idGenerator);
                LoadParser.parse(testResource, config, callback);
                assertTrue(callback.hasBeenFlushed());
                assertEquals(29, callback.getStartedElements().size());
                assertTrue(hasParsedElement(callback, "Product"));
                assertTrue(hasParsedElement(callback, "Features"));
                assertTrue(hasParsedCharacters(callback, "porttitor pharetra quis sed risus."));
                assertEquals("1", callback.getId());
            }/*  www  . j a  va 2 s . c o m*/
        }
    };

    for (int i = 0; i < threadNumber; i++) {
        threads.add(new Thread(runnable));
    }

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

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

From source file:org.terasoluna.gfw.common.exception.ResultMessagesLoggingInterceptorTest.java

/**
 * [invoke] Case of not occur exception in a multit-hreaded environment.
 * <p>//  w w  w .  j a v a  2s  .  c o m
 * [Expected Result]
 * <ol>
 * <li>method's return value is expected.</li>
 * <li>log is not output.</li>
 * </ol>
 * </p>
 */
@Test
public void testInvoke_multi_thread_not_occur_exception() throws Throwable {

    // do setup for test case.
    final TestFacade facade = getApplicationContext().getBean(TestFacade.class);
    final Map<Thread, String> actualMessage = new HashMap<Thread, String>();

    // setup for thread1.
    Thread thread1 = new Thread(new Runnable() {
        @Override
        public void run() {
            facade.setSleepTime(Long.valueOf(2));
            actualMessage.put(Thread.currentThread(), facade.getMessage());
        }
    });

    // setup for thread2.
    Thread thread2 = new Thread(new Runnable() {
        @Override
        public void run() {
            facade.setSleepTime(Long.valueOf(0));
            actualMessage.put(Thread.currentThread(), facade.getMessage());
        }
    });

    // do test.
    thread1.start();
    TimeUnit.SECONDS.sleep(1);
    thread2.start();

    // wait thread finish.
    thread1.join();
    thread2.join();

    // do assert.
    String expectedBaseMessage = getApplicationContext().getBean(TestRepository.class).toString() + ":";
    assertThat(actualMessage.get(thread1), is(expectedBaseMessage + thread1.getId()));
    assertThat(actualMessage.get(thread2), is(expectedBaseMessage + thread2.getId()));

    verify(mockExceptionLogger, never()).warn((Exception) any());
}