List of usage examples for java.lang Thread join
public final void join() throws InterruptedException
From source file:com.npower.dm.setup.task.DeviceGeneratorTask.java
@Override protected void process() throws SetupException { int totalThread = this.getTotalThread(); this.getSetup().getConsole().println("Starting total thread: " + totalThread); try {//from www.j a v a 2s . c om ThreadGroup group = new ThreadGroup(this.getClass().getName()); List<Thread> threads = new ArrayList<Thread>(); for (int i = 0; i < totalThread; i++) { TaskThread runnable = new TaskThread(this); Thread t = new Thread(group, runnable, "Thread#" + i); threads.add(t); t.start(); this.getSetup().getConsole().println("Started thread: " + t.getName()); } /* try { Thread.sleep(1000 * 3600 * 12); } catch (InterruptedException e) { e.printStackTrace(); } */ for (Thread t : threads) { t.join(); } } catch (InterruptedException e) { throw new SetupException(e.getMessage(), e); } this.getSetup().getConsole().println("All of thread finished."); }
From source file:com.predic8.membrane.core.interceptor.apimanagement.AMRateLimitInterceptorTest.java
@Test public void testHandleRequestRateLimit5SecondConcurrency() throws Exception { final Exchange exc = new Exchange(null); exc.setResponse(Response.ResponseBuilder.newInstance().build()); exc.setProperty(Exchange.API_KEY, "junit"); exc.setRule(new ServiceProxy()); exc.getRule().setName("junit API"); final AMRateLimiter rli = new AMRateLimiter(); ApiManagementConfiguration amc = new ApiManagementConfiguration(System.getProperty("user.dir"), "src\\test\\resources\\apimanagement\\api.yaml"); rli.setAmc(amc);/*w w w. j ava2 s. c o m*/ ArrayList<Thread> threads = new ArrayList<Thread>(); final AtomicInteger continues = new AtomicInteger(); final AtomicInteger returns = new AtomicInteger(); for (int i = 0; i < 1000; i++) { Thread t = new Thread(new Runnable() { @Override public void run() { try { Outcome out = rli.handleRequest(exc); if (out == Outcome.CONTINUE) { continues.incrementAndGet(); } else if (out == Outcome.RETURN) { returns.incrementAndGet(); } } catch (Exception e) { e.printStackTrace(); } } }); threads.add(t); t.start(); } for (Thread t : threads) { t.join(); } assertEquals(5, continues.get()); assertEquals(995, returns.get()); Thread.sleep(2000); assertEquals(Outcome.CONTINUE, rli.handleRequest(exc)); }
From source file:com.esri.geoevent.test.tools.RunTcpInTcpOutTest.java
private void runTest(long numberEvents, int rate, String server, int in_port, int out_port, Boolean isSingle) { this.send_rate = -1.0; this.rcv_rate = -1.0; try {/*from www . ja v a 2s .co m*/ TCPSocketReadRunnable reader = new TCPSocketReadRunnable(server, out_port, numberEvents, isSingle); Thread readerThread = new Thread(reader); readerThread.start(); int secs = 0; while (!reader.getReady()) { Thread.sleep(1000); secs += 1; if (secs > 10) { throw new Exception( "Starting Read Thread timeout. Make sure target server is started and firewall is open for ports specified."); } } this.send(server, in_port, numberEvents, rate); readerThread.join(); rcv_rate = reader.getAverage_read_per_second(); if (reader.getNum_events_read() != numberEvents) { // Number of events read differs from send throw new Exception("Number of Events received (" + Long.toString(reader.getNum_events_read()) + ") did not match what was sent (" + Long.toString(numberEvents) + ") !"); } } catch (Exception e) { System.out.println(e.getMessage()); rcv_rate = -1.0; } finally { this.rcv_rate = rcv_rate; } }
From source file:bq.jpa.demo.lock.LockTester.java
public void test2() { System.out.println("---- modify salary while do statistic ----"); Thread thread1 = new Thread(new Runnable() { @Override/*from ww w . ja va 2s. c o m*/ public void run() { service.doStatisticSalary(); } }); Thread thread2 = new Thread(new Runnable() { @Override public void run() { service.doUpdateSalary(); } }); thread1.setName("dostatistic"); thread2.setName("doupdatesalary"); try { thread2.start(); Thread.sleep(1000); thread1.start(); } catch (InterruptedException e1) { } try { thread1.join(); thread2.join(); } catch (InterruptedException e) { } }
From source file:eu.stratosphere.pact.runtime.task.CombineTaskTest.java
@Test public void testCancelCombineTaskSorting() { super.initEnvironment(3 * 1024 * 1024); super.addInput(new DelayingInfinitiveInputIterator(100), 1); super.addOutput(new NirvanaOutputList()); final CombineTask<PactRecord> testTask = new CombineTask<PactRecord>(); super.getTaskConfig().setLocalStrategy(LocalStrategy.COMBININGSORT); super.getTaskConfig().setMemorySize(3 * 1024 * 1024); super.getTaskConfig().setNumFilehandles(2); final int[] keyPos = 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), keyPos, keyClasses); super.registerTask(testTask, MockFailingCombiningReduceStub.class); Thread taskRunner = new Thread() { @Override//from w ww.java2 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:com.meltmedia.dropwizard.etcd.json.EtcdWatchServiceIT.java
@Test public void shouldHandleNoiseInSimilarPaths() throws InterruptedException { int eventsCount = 100; // add a directory watch. WatchService service = serviceRule.getService(); @SuppressWarnings("unchecked") EtcdEventHandler<NodeData> handler = mock(EtcdEventHandler.class); EtcdDirectoryDao<NodeData> dirDao = new EtcdDirectoryDao<NodeData>(clientRule::getClient, BASE_PATH + "/dir", mapper, NODE_DATA_TYPE); EtcdDirectoryDao<NoiseDocument> noiseDao = new EtcdDirectoryDao<NoiseDocument>(clientRule::getClient, BASE_PATH + "/directory", mapper, new TypeReference<NoiseDocument>() { });/*from w w w.ja v a2 s. c om*/ Thread events = startNodeDataThread(dirDao, eventsCount); Thread noise = startNoiseThread(noiseDao, eventsCount); try { service.registerDirectoryWatch("/dir", new TypeReference<NodeData>() { }, handler); verifySequentialNodeData(handler, eventsCount); } finally { events.join(); noise.join(); } }
From source file:com.meltmedia.dropwizard.etcd.json.EtcdWatchServiceIT.java
@Test public void shouldHandleNoiseInSubPaths() throws InterruptedException { int eventsCount = 100; // add a directory watch. WatchService service = serviceRule.getService(); @SuppressWarnings("unchecked") EtcdEventHandler<NodeData> handler = mock(EtcdEventHandler.class); EtcdDirectoryDao<NodeData> dirDao = new EtcdDirectoryDao<NodeData>(clientRule::getClient, BASE_PATH + "/dir", mapper, NODE_DATA_TYPE); EtcdDirectoryDao<NoiseDocument> noiseDao = new EtcdDirectoryDao<NoiseDocument>(clientRule::getClient, BASE_PATH + "/dir/sub", mapper, new TypeReference<NoiseDocument>() { });/*from w ww.j a va 2s.c om*/ Thread events = startNodeDataThread(dirDao, eventsCount); Thread noise = startNoiseThread(noiseDao, eventsCount); try { service.registerDirectoryWatch("/dir", new TypeReference<NodeData>() { }, handler); verifySequentialNodeData(handler, eventsCount); } finally { events.join(); noise.join(); } }
From source file:com.meltmedia.dropwizard.etcd.json.EtcdWatchServiceIT.java
@Test public void shouldIgnoreEventsInSubPaths() throws InterruptedException { int eventsCount = 100; // add a directory watch. WatchService service = serviceRule.getService(); @SuppressWarnings("unchecked") EtcdEventHandler<NodeData> handler = mock(EtcdEventHandler.class); EtcdDirectoryDao<NodeData> dirDao = new EtcdDirectoryDao<NodeData>(clientRule::getClient, BASE_PATH + "/dir", mapper, NODE_DATA_TYPE); EtcdDirectoryDao<NodeData> subDao = new EtcdDirectoryDao<NodeData>(clientRule::getClient, BASE_PATH + "/dir/sub", mapper, NODE_DATA_TYPE); Thread events = startNodeDataThread(dirDao, eventsCount); Thread noise = startNodeDataThread(subDao, eventsCount); try {//from w w w.java 2 s. c o m service.registerDirectoryWatch("/dir", new TypeReference<NodeData>() { }, handler); verifySequentialNodeData(handler, eventsCount); } finally { events.join(); noise.join(); } }
From source file:it.geosolutions.tools.io.CopyTreeTest.java
@Test public void stopCopyTest() throws Exception { LOGGER.info("BEGIN: stopCopyTest"); File srcMount = TestData.file(this, "."); final CopyTree act = new CopyTree( FileFilterUtils.or(FileFilterUtils.directoryFileFilter(), FileFilterUtils.fileFileFilter()), cs, srcMount, destMount);//ww w.j av a2 s. c o m final Thread copier = new Thread(new Runnable() { public void run() { act.setCancelled(); try { Assert.assertEquals("Returned list should be null", 0, act.copy()); } catch (IllegalStateException e) { e.printStackTrace(); Assert.fail(e.getLocalizedMessage()); } catch (IOException e) { e.printStackTrace(); Assert.fail(e.getLocalizedMessage()); } } }); copier.start(); try { copier.join(); } catch (InterruptedException e) { LOGGER.info(e.getLocalizedMessage(), e); Assert.fail(); } LOGGER.info("STOP: stopCopyTest"); }
From source file:bq.jpa.demo.lock.LockTester.java
public void test3() { System.out.println("---- modify salary while do statistic ----"); Thread thread1 = new Thread(new Runnable() { @Override// w ww. ja v a 2s. co m public void run() { service.doModify1(); } }); Thread thread2 = new Thread(new Runnable() { @Override public void run() { service.doModify2(); } }); thread1.setName("modifythread1"); thread2.setName("modifythread2"); try { thread2.start(); Thread.sleep(1000); thread1.start(); } catch (InterruptedException e1) { e1.printStackTrace(); } try { thread1.join(); thread2.join(); } catch (InterruptedException e) { e.printStackTrace(); } }