Here are the examples of the java api class java.util.concurrent.ExecutorService taken from open source projects.
1. MainActivity#runThreadPool()
View licenseprivate void runThreadPool() { Runnable command = new Runnable() { @Override public void run() { SystemClock.sleep(2000); } }; ExecutorService fixedThreadPool = Executors.newFixedThreadPool(4); fixedThreadPool.execute(command); ExecutorService cachedThreadPool = Executors.newCachedThreadPool(); cachedThreadPool.execute(command); ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(4); // 2000ms???command scheduledThreadPool.schedule(command, 2000, TimeUnit.MILLISECONDS); // ??10ms????1000ms????command scheduledThreadPool.scheduleAtFixedRate(command, 10, 1000, TimeUnit.MILLISECONDS); ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor(); singleThreadExecutor.execute(command); }
2. PackageFactoryTest#testCreatePackageIsolatedFromOuterErrors()
View license@Test public void testCreatePackageIsolatedFromOuterErrors() throws Exception { ExecutorService e = Executors.newCachedThreadPool(); final Semaphore beforeError = new Semaphore(0); final Semaphore afterError = new Semaphore(0); Reporter reporter = new Reporter(); ParsingTracker parser = new ParsingTracker(beforeError, afterError, reporter); final Logger log = Logger.getLogger(PackageFactory.class.getName()); log.addHandler(parser); Level originalLevel = log.getLevel(); log.setLevel(Level.FINE); e.execute(new ErrorReporter(reporter, beforeError, afterError)); e.execute(parser); // wait for all to finish e.shutdown(); assertTrue(e.awaitTermination(TestUtils.WAIT_TIMEOUT_MILLISECONDS, TimeUnit.MILLISECONDS)); log.removeHandler(parser); log.setLevel(originalLevel); assertTrue(parser.hasParsed()); }
3. ShouldNotDeadlockAnswerExecutionTest#failIfMockIsSharedBetweenThreads()
View license@Test public void failIfMockIsSharedBetweenThreads() throws Exception { Service service = Mockito.mock(Service.class); ExecutorService threads = Executors.newCachedThreadPool(); AtomicInteger counter = new AtomicInteger(2); // registed answer on verySlowMethod Mockito.when(service.verySlowMethod()).thenAnswer(new LockingAnswer(counter)); // execute verySlowMethod twice in separate threads threads.execute(new ServiceRunner(service)); threads.execute(new ServiceRunner(service)); // waiting for threads to finish threads.shutdown(); if (!threads.awaitTermination(1000, TimeUnit.MILLISECONDS)) { // threads were timed-out Assert.fail(); } }
4. ParallelIterate2Test#creationAndExecution()
View license/** * crude test to check that creation works and that all tasks are executed */ @Test public void creationAndExecution() throws InterruptedException { int howManyTimes = 200; AtomicInteger counter = new AtomicInteger(0); Collection<Callable<Integer>> tasks = new ArrayList<>(); Interval.oneTo(howManyTimes).run(() -> tasks.add(counter::getAndIncrement)); ExecutorService executorService1 = ParallelIterate.newPooledExecutor(4, "test pool 2 4", true); executorService1.invokeAll(tasks); Assert.assertEquals(howManyTimes, counter.get()); counter.set(0); ExecutorService executorService2 = ParallelIterate.newPooledExecutor(2, "test pool 2", true); executorService2.invokeAll(tasks); Assert.assertEquals(howManyTimes, counter.get()); }
5. ParallelIterate2Test#creationAndExecution()
View license/** * crude test to check that creation works and that all tasks are executed */ @Test public void creationAndExecution() throws InterruptedException { int howManyTimes = 200; AtomicInteger counter = new AtomicInteger(0); Collection<Callable<Integer>> tasks = new ArrayList<>(); Interval.oneTo(howManyTimes).run(() -> tasks.add(counter::getAndIncrement)); ExecutorService executorService1 = ParallelIterate.newPooledExecutor(4, "test pool 2 4", true); executorService1.invokeAll(tasks); Assert.assertEquals(howManyTimes, counter.get()); counter.set(0); ExecutorService executorService2 = ParallelIterate.newPooledExecutor(2, "test pool 2", true); executorService2.invokeAll(tasks); Assert.assertEquals(howManyTimes, counter.get()); }
6. IgniteExecutorServiceTest#testExecute()
View license/** * @throws Exception Thrown in case of test failure. */ public void testExecute() throws Exception { Ignite ignite = G.ignite(getTestGridName()); ExecutorService srvc = createExecutorService(ignite); srvc.execute(new Runnable() { @IgniteInstanceResource private Ignite ignite; @Override public void run() { System.out.println("Test message."); assert this.ignite != null; } }); srvc.execute(new TestRunnable()); srvc.shutdown(); }
7. CrawlerStopTest#whenStopIsCalledTheCrawlerStopsGracefully()
View license@Test(timeout = 60_000) public void whenStopIsCalledTheCrawlerStopsGracefully() throws Exception { CrawljaxConfigurationBuilder builder = SERVER.newConfigBuilder("infinite.html"); CrawljaxRunner runner = new CrawljaxRunner(builder.setUnlimitedCrawlDepth().setUnlimitedCrawlDepth().setUnlimitedStates().build()); ExecutorService executor = Executors.newSingleThreadExecutor(); executor.submit(runner); Thread.sleep(TimeUnit.SECONDS.toMillis(15)); runner.stop(); executor.shutdown(); executor.awaitTermination(30, TimeUnit.SECONDS); assertThat(runner.getReason(), is(ExitStatus.STOPPED)); }
8. SolrCoreTest#testReloadLeak()
View license/** * Test that's meant to be run with many iterations to expose a leak of SolrIndexSearcher when a core is closed * due to a reload. Without the fix, this test fails with most iters=1000 runs. */ @Test public void testReloadLeak() throws Exception { final ExecutorService executor = ExecutorUtil.newMDCAwareFixedThreadPool(1, new DefaultSolrThreadFactory("testReloadLeak")); // Continuously open new searcher while core is not closed, and reload core to try to reproduce searcher leak. // While in practice we never continuously open new searchers, this is trying to make up for the fact that opening // a searcher in this empty core is very fast by opening new searchers continuously to increase the likelihood // for race. SolrCore core = h.getCore(); assertTrue("Refcount != 1", core.getOpenCount() == 1); executor.execute(new NewSearcherRunnable(core)); // Since we called getCore() vs getCoreInc() and don't own a refCount, the container should decRef the core // and close it when we call reload. h.reload(); executor.shutdown(); executor.awaitTermination(1, TimeUnit.MINUTES); // Check that all cores are closed and no searcher references are leaked. assertTrue("SolrCore " + core + " is not closed", core.isClosed()); assertTrue(core.areAllSearcherReferencesEmpty()); }
9. RedisBFPerformance#dumbParallelAdds()
View licenseprivate static void dumbParallelAdds(int count, int items, BloomFilter<String> b) throws Exception { b.clear(); Random r = new Random(); ExecutorService exec = Executors.newFixedThreadPool(20); long start = System.currentTimeMillis(); IntStream.range(0, count).forEach( i -> { exec.submit(() -> b.add(String.valueOf(r.nextInt(items)))); }); exec.shutdown(); exec.awaitTermination(5, TimeUnit.SECONDS); long end = System.currentTimeMillis(); MemoryBFTest.printStat(start, end, count); }
10. ConcurrentJcrResourceBundleLoadingTest#loadBundlesOnlyOnceWithConcurrentRequests()
View license@Test public void loadBundlesOnlyOnceWithConcurrentRequests() throws Exception { final int numberOfThreads = 40; final ExecutorService executor = Executors.newFixedThreadPool(numberOfThreads / 2); for (int i = 0; i < numberOfThreads; i++) { final Locale language = i < numberOfThreads / 2 ? Locale.ENGLISH : Locale.GERMAN; executor.submit(new Runnable() { @Override public void run() { provider.getResourceBundle(language); } }); } executor.shutdown(); executor.awaitTermination(5, TimeUnit.SECONDS); verifyPrivate(provider, times(1)).invoke("createResourceBundle", eq(null), eq(Locale.ENGLISH)); verifyPrivate(provider, times(1)).invoke("createResourceBundle", eq(null), eq(Locale.GERMAN)); }
11. ThreadScopeSessionBeanTest#ShouldUseDifferentSessionForSessionBeansOnDifferentThreads()
View license@Test public void ShouldUseDifferentSessionForSessionBeansOnDifferentThreads() throws Exception { ExecutorService executor = Executors.newFixedThreadPool(10); for (int i = 0; i < 2; i++) { final int j = i; executor.submit(new Runnable() { @Override public void run() { // captures the session's JVM identity sessions[j] = session.toString(); } }); } executor.shutdown(); executor.awaitTermination(1, TimeUnit.MINUTES); Assert.assertFalse(sessions[0].equals(sessions[1])); }
12. LongOpOrderTest#testOrdering()
View license@Test public void testOrdering() throws InterruptedException { errors.set(0); Thread.setDefaultUncaughtExceptionHandler(handler); final ExecutorService exec = Executors.newCachedThreadPool(new NamedThreadFactory("checker")); final ScheduledExecutorService checker = Executors.newScheduledThreadPool(1, new NamedThreadFactory("checker")); for (int i = 0; i < CONSUMERS; i++) new TestOrdering(exec, checker); exec.shutdown(); exec.awaitTermination((long) (RUNTIME * 1.1), TimeUnit.MILLISECONDS); assertTrue(exec.isShutdown()); assertTrue(errors.get() == 0); }
13. TestAsyncSuroSender#testMultithread()
View license@Test public void testMultithread() throws InterruptedException { AsyncSuroClient client = injector.getInstance(AsyncSuroClient.class); ExecutorService executors = Executors.newFixedThreadPool(3); for (int i = 0; i < 10; ++i) { executors.submit(new AsyncSuroSender(TestConnectionPool.createMessageSet(100), client, injector.getInstance(ClientConfig.class))); } executors.shutdown(); executors.awaitTermination(10, TimeUnit.SECONDS); TestConnectionPool.checkMessageSetCount(servers, 10, true); assertEquals(client.getSentMessageCount(), 1000); }
14. StarTreeClusterIntegrationTest#generateAndUploadSegments()
View license/** * Generate the reference and star tree indexes and upload to corresponding tables. * @param avroFiles * @param tableName * @param starTree * @throws IOException * @throws ArchiveException * @throws InterruptedException */ private void generateAndUploadSegments(List<File> avroFiles, String tableName, boolean starTree) throws IOException, ArchiveException, InterruptedException { BaseClusterIntegrationTest.ensureDirectoryExistsAndIsEmpty(_segmentsDir); BaseClusterIntegrationTest.ensureDirectoryExistsAndIsEmpty(_tarredSegmentsDir); ExecutorService executor = Executors.newCachedThreadPool(); BaseClusterIntegrationTest.buildSegmentsFromAvro(avroFiles, executor, 0, _segmentsDir, _tarredSegmentsDir, tableName, starTree, null); executor.shutdown(); executor.awaitTermination(TIMEOUT_IN_SECONDS, TimeUnit.SECONDS); for (String segmentName : _tarredSegmentsDir.list()) { LOGGER.info("Uploading segment {}", segmentName); File file = new File(_tarredSegmentsDir, segmentName); FileUploadUtils.sendSegmentFile(ControllerTestUtils.DEFAULT_CONTROLLER_HOST, ControllerTestUtils.DEFAULT_CONTROLLER_API_PORT, segmentName, new FileInputStream(file), file.length()); } }
15. ScanBasedQueryProcessor#processSegments()
View licenseprivate List<ResultTable> processSegments(final String query, final BrokerRequest brokerRequest) throws InterruptedException { ExecutorService executorService = Executors.newFixedThreadPool(10); final List<ResultTable> resultTables = Collections.synchronizedList(new ArrayList<ResultTable>()); for (final SegmentQueryProcessor segmentQueryProcessor : _segmentQueryProcessorMap.values()) { executorService.execute(new Runnable() { @Override public void run() { try { ResultTable resultTable = segmentQueryProcessor.process(brokerRequest); if (resultTable != null) { resultTables.add(resultTable); } } catch (Exception e) { LOGGER.error("Exception caught while processing segment '{}'.", segmentQueryProcessor.getSegmentName(), e); return; } } }); } executorService.shutdown(); executorService.awaitTermination(_timeoutInSeconds, TimeUnit.SECONDS); return resultTables; }
16. TicksTest#executorTest()
View license@Test public void executorTest() throws InterruptedException, ExecutionException { ExecutorService executor = Executors.newSingleThreadExecutor(); CompletableFuture<Integer> nTicks = new CompletableFuture<>(); executor.execute(() -> { EventCounter counter = new EventCounter(); Subscription sub = EventStreams.ticks(Duration.ofMillis(100), scheduler, executor).subscribe(counter::accept); // stop after 3 ticks ScheduledExecutorServiceTimer.create(Duration.ofMillis(350), sub::unsubscribe, scheduler, executor).restart(); // wait a little more to test that no more than 3 ticks arrive anyway ScheduledExecutorServiceTimer.create(Duration.ofMillis(550), () -> nTicks.complete(counter.get()), scheduler, executor).restart(); }); assertEquals(3, nTicks.get().intValue()); executor.shutdown(); }
17. DirectProducerBlockingTest#testProducerBlocksResumeTest()
View licensepublic void testProducerBlocksResumeTest() throws Exception { context.suspendRoute("foo"); ExecutorService executor = Executors.newSingleThreadExecutor(); executor.submit(new Runnable() { @Override public void run() { try { Thread.sleep(2000); log.info("Resuming consumer"); context.resumeRoute("foo"); } catch (Exception e) { } } }); getMockEndpoint("mock:result").expectedMessageCount(1); template.sendBody("direct:suspended?block=true&timeout=5000", "hello world"); assertMockEndpointsSatisfied(); executor.shutdownNow(); }
18. DirectVmProducerBlockingTest#testProducerBlocksResumeTest()
View licensepublic void testProducerBlocksResumeTest() throws Exception { context.suspendRoute("foo"); ExecutorService executor = Executors.newSingleThreadExecutor(); executor.submit(new Runnable() { @Override public void run() { try { Thread.sleep(2000); log.info("Resuming consumer"); context.resumeRoute("foo"); } catch (Exception e) { } } }); getMockEndpoint("mock:result").expectedMessageCount(1); template.sendBody("direct-vm:suspended?block=true&timeout=5000", "hello world"); assertMockEndpointsSatisfied(); executor.shutdownNow(); }
19. CoreClientTest#testCoreClientWithInjectedThreadPools()
View license@Test public void testCoreClientWithInjectedThreadPools() throws Exception { ExecutorService threadPool = Executors.newCachedThreadPool(ActiveMQThreadFactory.defaultThreadFactory()); ScheduledThreadPoolExecutor scheduledThreadPool = new ScheduledThreadPoolExecutor(10); ServerLocator locator = createNonHALocator(false); boolean setThreadPools = locator.setThreadPools(threadPool, scheduledThreadPool); assertTrue(setThreadPools); testCoreClient(true, locator); threadPool.shutdown(); scheduledThreadPool.shutdown(); threadPool.awaitTermination(60, TimeUnit.SECONDS); scheduledThreadPool.awaitTermination(60, TimeUnit.SECONDS); }
20. BlueprintTest#testCoordinationLifecycle()
View license@Test public void testCoordinationLifecycle() throws InterruptedException, ExecutionException { CarService carService = getCarService("em"); assertNoCars(carService); Runnable carLifeCycle = getService(Runnable.class, "(type=carCoordinated)"); carLifeCycle.run(); ExecutorService exec = Executors.newFixedThreadPool(20); List<Future<?>> futures = new ArrayList<Future<?>>(); for (int c = 0; c < 100; c++) { futures.add(exec.submit(carLifeCycle)); } exec.shutdown(); exec.awaitTermination(30, TimeUnit.SECONDS); for (Future<?> future : futures) { future.get(); } assertNoCars(carService); }
21. BlueprintTest#testCoordinationLifecycle()
View license@Test public void testCoordinationLifecycle() throws InterruptedException, ExecutionException { CarService carService = getCarService("em"); assertNoCars(carService); Runnable carLifeCycle = getService(Runnable.class, "(type=carCoordinated)"); carLifeCycle.run(); ExecutorService exec = Executors.newFixedThreadPool(20); List<Future<?>> futures = new ArrayList<Future<?>>(); for (int c = 0; c < 100; c++) { futures.add(exec.submit(carLifeCycle)); } exec.shutdown(); exec.awaitTermination(30, TimeUnit.SECONDS); for (Future<?> future : futures) { future.get(); } assertNoCars(carService); }
22. TcpManyClientConnectionsTest#test()
View license/** * test many clients connecting to a single server */ @Test public void test() throws IOException, InterruptedException { final ExecutorService executorService = Executors.newCachedThreadPool(); for (int i = 0; i < MAX; i++) { final int j = i; executorService.execute(() -> { maps[j].put("hello" + j, "world" + j); Assert.assertEquals("world" + j, maps[j].get("hello" + j)); }); } executorService.shutdown(); executorService.awaitTermination(2, TimeUnit.SECONDS); }
23. ConcurrentModelTest#testConcurrentInsert()
View license@Test public void testConcurrentInsert() throws InterruptedException { Delete.table(TestModel1.class); ExecutorService executorService = Executors.newFixedThreadPool(3); for (int i = 0; i < CONCURRENT_INSERT_COUNT; i++) { // fails executorService.execute(new InsertRunnable()); //executorService.execute(new PlainSqlInsertRunnable()); // passes } executorService.shutdown(); executorService.awaitTermination(CONCURRENT_INSERT_TIMEOUT, TimeUnit.MILLISECONDS); long modelCount = selectCountOf().from(TestModel1.class).count(); assertEquals(CONCURRENT_INSERT_COUNT, modelCount); }
24. GraphDatabaseStressTest#testConcurrentUses()
View license@Test public void testConcurrentUses() throws Exception { ConcurrentLinkedQueue<String> errorLog = new ConcurrentLinkedQueue<String>(); // ExecutorService executor = Executors.newCachedThreadPool(); ExecutorService executor = Executors.newSingleThreadExecutor(); List<Future<?>> futures = new ArrayList<Future<?>>(); for (String s : new String[] { "a", "b", "c", "d" }) { Runnable task = new InsertMany(s, errorLog); futures.add(executor.submit(task)); } for (Future<?> f : futures) { f.get(); } executor.shutdown(); executor.awaitTermination(10, TimeUnit.SECONDS); assertEquals(errorLog.toString(), 0, errorLog.size()); assertEquals(100, database.getDepth(ObjectId.forString("a_commit_100"))); }
25. HawkBuilder#build()
View licensepublic void build(final Callback callback) { ExecutorService executor = Executors.newSingleThreadExecutor(); executor.execute(new Runnable() { @Override public void run() { try { startBuild(); callback.onSuccess(); } catch (Exception e) { callback.onFail(e); } } }); executor.shutdown(); }
26. AggregateConcurrentPerCorrelationKeyTest#testAggregateConcurrentPerCorrelationKey()
View licensepublic void testAggregateConcurrentPerCorrelationKey() throws Exception { ExecutorService service = Executors.newFixedThreadPool(20); List<Callable<Object>> tasks = new ArrayList<Callable<Object>>(); for (int i = 0; i < size; i++) { final int id = i % 5; final int count = i; tasks.add(new Callable<Object>() { public Object call() throws Exception { template.sendBodyAndHeader(uri, "" + count, "id", id); return null; } }); } MockEndpoint mock = getMockEndpoint("mock:result"); mock.expectedMessageCount(5); // submit all tasks service.invokeAll(tasks); assertMockEndpointsSatisfied(); service.shutdownNow(); }
27. StreamResequencerTest#testMultithreaded()
View licensepublic void testMultithreaded() throws Exception { int numMessages = 100; Object[] bodies = new Object[numMessages]; for (int i = 0; i < numMessages; i++) { bodies[i] = "msg" + i; } getMockEndpoint("mock:result").expectedBodiesReceived(bodies); getMockEndpoint("mock:result").setResultWaitTime(20000); ProducerTemplate producerTemplate = context.createProducerTemplate(); ProducerTemplate producerTemplate2 = context.createProducerTemplate(); ExecutorService service = context.getExecutorServiceManager().newFixedThreadPool(this, getName(), 2); service.execute(new Sender(producerTemplate, 0, numMessages, 2)); service.execute(new Sender(producerTemplate2, 1, numMessages, 2)); assertMockEndpointsSatisfied(); ServiceHelper.stopServices(producerTemplate, producerTemplate2); }
28. SynchronousExecutorServiceTest#testSynchronousExecutorServiceShutdown()
View licensepublic void testSynchronousExecutorServiceShutdown() throws Exception { ExecutorService service = new SynchronousExecutorService(); service.execute(new Runnable() { public void run() { invoked = true; } }); service.shutdown(); assertTrue(service.isShutdown()); assertTrue(service.isTerminated()); }
29. LongOpOrderTest#testOrdering()
View license@Test public void testOrdering() throws InterruptedException { errors.set(0); Thread.setDefaultUncaughtExceptionHandler(handler); final ExecutorService exec = Executors.newCachedThreadPool(new NamedThreadFactory("checker")); final ScheduledExecutorService checker = Executors.newScheduledThreadPool(1, new NamedThreadFactory("checker")); for (int i = 0; i < CONSUMERS; i++) new TestOrdering(exec, checker); exec.shutdown(); exec.awaitTermination((long) (RUNTIME * 1.1), TimeUnit.MILLISECONDS); assertTrue(exec.isShutdown()); assertTrue(errors.get() == 0); }
30. HealthCheckRegistryTest#runsRegisteredHealthChecksInParallel()
View license@Test public void runsRegisteredHealthChecksInParallel() throws Exception { final ExecutorService executor = Executors.newFixedThreadPool(10); final Map<String, HealthCheck.Result> results = registry.runHealthChecks(executor); executor.shutdown(); executor.awaitTermination(1, TimeUnit.SECONDS); assertThat(results).contains(entry("hc1", r1)); assertThat(results).contains(entry("hc2", r2)); }
31. HttpOverSpdyTest#synchronousSpdyRequest()
View license@Test @Ignore public void synchronousSpdyRequest() throws Exception { server.enqueue(new MockResponse().setBody("A")); server.enqueue(new MockResponse().setBody("A")); ExecutorService executor = Executors.newCachedThreadPool(); CountDownLatch countDownLatch = new CountDownLatch(2); executor.execute(new SpdyRequest("/r1", countDownLatch)); executor.execute(new SpdyRequest("/r2", countDownLatch)); countDownLatch.await(); assertEquals(0, server.takeRequest().getSequenceNumber()); assertEquals(1, server.takeRequest().getSequenceNumber()); }
32. BlockingBooleanTest#waitSame()
View license@Test public void waitSame() { BlockingBoolean b = new BlockingBoolean(true); CountDownLatch latch = new CountDownLatch(1); ExecutorService exec = Executors.newSingleThreadExecutor(); exec.submit(() -> { try { b.await(true); latch.countDown(); } catch (InterruptedException e) { fail(); } }); try { assertTrue(latch.await(TIMEOUT, TimeUnit.MILLISECONDS)); } catch (InterruptedException e) { fail(); } exec.shutdown(); }
33. BlockingBooleanTest#waitTimeout()
View license@Test public void waitTimeout() { BlockingBoolean b = new BlockingBoolean(true); CountDownLatch latch = new CountDownLatch(1); ExecutorService exec = Executors.newSingleThreadExecutor(); exec.submit(() -> { try { if (!b.await(false, 1, TimeUnit.NANOSECONDS)) { latch.countDown(); } else { fail(); } } catch (InterruptedException e) { fail(); } }); try { assertTrue(latch.await(TIMEOUT, TimeUnit.MILLISECONDS)); } catch (InterruptedException e) { fail(); } exec.shutdown(); }
34. ParkLoops#main()
View licensepublic static void main(String[] args) throws Exception { final SplittableRandom rnd = new SplittableRandom(); final ExecutorService pool = Executors.newCachedThreadPool(); final AtomicReferenceArray<Thread> threads = new AtomicReferenceArray<>(THREADS); final CountDownLatch done = new CountDownLatch(THREADS); for (int i = 0; i < THREADS; i++) { pool.submit(new Parker(threads, done, rnd.split())); pool.submit(new Unparker(threads, done, rnd.split())); } // Let test harness handle timeout done.await(); pool.shutdown(); pool.awaitTermination(Long.MAX_VALUE, SECONDS); }
35. CascadedValidationTest#testCascadedValidationMultiThreaded()
View license/** * To be executed manually. Not part of the JMeter tests for now. */ @Test public void testCascadedValidationMultiThreaded() throws Exception { CountDownLatch startLatch = new CountDownLatch(1); ExecutorService executor = Executors.newFixedThreadPool(SIZE_OF_THREAD_POOL); for (int i = 0; i <= NUMBER_OF_RUNNABLES; i++) { Runnable run = new TestRunner(startLatch); executor.execute(run); } executor.shutdown(); //start! startLatch.countDown(); executor.awaitTermination(600, TimeUnit.SECONDS); }
36. DataStoreUtil#importJobDataParallel()
View licenseprivate static void importJobDataParallel(List<String> jobIds, SpawnDataStore sourceDataStore, SpawnDataStore targetDataStore, boolean checkAllWrites) throws Exception { ExecutorService executorService = new ThreadPoolExecutor(numCutoverThreads, numCutoverThreads, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<>()); AtomicBoolean gotFailures = new AtomicBoolean(false); int partitionSize = Math.max((int) Math.ceil((double) jobIds.size() / numCutoverThreads), 1); for (List<String> partition : Lists.partition(jobIds, partitionSize)) { executorService.submit(new CutoverWorker(sourceDataStore, targetDataStore, gotFailures, partition, checkAllWrites)); } executorService.shutdown(); executorService.awaitTermination(cutoverTimeoutMinutes, TimeUnit.MINUTES); if (gotFailures.get()) { throw new RuntimeException("A cutover worker has failed; see log for details"); } }
37. GridThreadPoolExecutorServiceSelfTest#testSingleThreadExecutor()
View license/** * @throws Exception If failed. */ public void testSingleThreadExecutor() throws Exception { ExecutorService exec = Executors.newSingleThreadExecutor(); exec.submit(new InterruptingRunnable()).get(); // Thread is interrupted but Thread.interrupted() is called in AbstractQueuedSynchronizer.acquireInterruptibly // when blockingQueue wants to get the new task (see ThreadPoolExecutor.getTask()). // This will reset the interrupted flag. Any subsequent calls to Thread.currentThread.isInterrupted() // will return false. Future<Boolean> fut = exec.submit(new IsInterruptedAssertionCallable()); assert !fut.get() : "Expecting the executorService to reset the interrupted flag when reinvoking the thread"; exec.shutdown(); assert exec.awaitTermination(30, SECONDS); }
38. GridThreadPoolExecutorServiceSelfTest#testSingleGridThreadExecutor()
View license/** * @throws Exception If failed. */ public void testSingleGridThreadExecutor() throws Exception { ExecutorService exec = Executors.newSingleThreadExecutor(new IgniteThreadFactory("gridName")); exec.submit(new InterruptingRunnable()).get(); Future<Boolean> fut = exec.submit(new IsInterruptedAssertionCallable()); assert !fut.get() : "Expecting the executorService to reset the interrupted flag when reinvoking the thread"; // Thread is interrupted but Thread.interrupted() is called in AbstractQueuedSynchronizer.acquireInterruptibly // when blockingQueue wants to get the new task (see ThreadPoolExecutor.getTask()). // This will reset the interrupted flag but not the one from GridThread. Any subsequent calls to // Thread.currentThread.isInterrupted() will return true; exec.shutdown(); assert exec.awaitTermination(30, SECONDS); }
39. FillupMatrix#main()
View licensepublic static void main(String args[]) throws InterruptedException { int size = 5000; FillupMatrix fum = new FillupMatrix(size); ExecutorService executor = Executors.newFixedThreadPool(10); for (int i = 0; i < size * size; i++) { executor.execute(() -> fum.updateMatrix()); } executor.shutdown(); executor.awaitTermination(10, TimeUnit.SECONDS); for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { assert fum.getVal(i, j); } } }
40. MinMaxKeeper#main()
View licensepublic static void main(String args[]) throws InterruptedException { ExecutorService executors = Executors.newFixedThreadPool(100); MinMaxKeeper mmKeeper = new MinMaxKeeper(); for (int i = 0; i < 100000; i++) { GenerateRand rand = new GenerateRand(mmKeeper, i); executors.execute(rand); } executors.shutdown(); executors.awaitTermination(10, TimeUnit.SECONDS); assert mmKeeper.getMin() == -1; assert mmKeeper.getMax() == 1000001; }
41. LinkedTransferQueueTest#testOfferInExecutor()
View license/** * offer transfers elements across Executor tasks */ public void testOfferInExecutor() { final LinkedTransferQueue q = new LinkedTransferQueue(); final CheckedBarrier threadsStarted = new CheckedBarrier(2); ExecutorService executor = Executors.newFixedThreadPool(2); executor.execute(new CheckedRunnable() { public void realRun() throws InterruptedException { threadsStarted.await(); assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS)); } }); executor.execute(new CheckedRunnable() { public void realRun() throws InterruptedException { threadsStarted.await(); assertSame(one, q.take()); checkEmpty(q); } }); joinPool(executor); }
42. LinkedTransferQueueTest#testPollInExecutor()
View license/** * timed poll retrieves elements across Executor threads */ public void testPollInExecutor() { final LinkedTransferQueue q = new LinkedTransferQueue(); final CheckedBarrier threadsStarted = new CheckedBarrier(2); ExecutorService executor = Executors.newFixedThreadPool(2); executor.execute(new CheckedRunnable() { public void realRun() throws InterruptedException { assertNull(q.poll()); threadsStarted.await(); assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); checkEmpty(q); } }); executor.execute(new CheckedRunnable() { public void realRun() throws InterruptedException { threadsStarted.await(); q.put(one); } }); joinPool(executor); }
43. ConcurrentWriteMultipleMkMongoTest#doTest()
View licenseprivate void doTest(int numberOfNodes) throws Exception { int numberOfChildren = 10; int numberOfMks = 3; String[] prefixes = new String[] { "a", "b", "c", "d", "e", "f" }; ExecutorService executor = Executors.newFixedThreadPool(numberOfMks); List<DocumentMK> mks = new ArrayList<DocumentMK>(); for (int i = 0; i < numberOfMks; i++) { String diff = buildPyramidDiff("/", 0, numberOfChildren, numberOfNodes, prefixes[i], new StringBuilder()).toString(); DocumentMK mk = new DocumentMK.Builder().open(); mks.add(mk); GenericWriteTask task = new GenericWriteTask("mk-" + i, mk, diff, 10); executor.execute(task); } executor.shutdown(); executor.awaitTermination(10, TimeUnit.MINUTES); for (DocumentMK mk : mks) { mk.dispose(); } }
44. JUConcurrentTests#futureTask()
View license@RaceTest(expectRace = false, description = "Test happens-before relations between FutureTask calculation and get") public void futureTask() { FutureTask<int[]> future = new FutureTask<int[]>(new Callable<int[]>() { public int[] call() { int[] res = new int[1]; res[0] = 42; return res; } }); ExecutorService executor = Executors.newSingleThreadExecutor(); executor.execute(future); try { int[] futureRes = future.get(); futureRes[0]++; } catch (Exception e) { throw new RuntimeException(e); } executor.shutdown(); }
45. Lock2#main()
View licensepublic static void main(String[] args) { ExecutorService executor = Executors.newFixedThreadPool(2); ReentrantLock lock = new ReentrantLock(); executor.submit(() -> { lock.lock(); try { ConcurrentUtils.sleep(1); } finally { lock.unlock(); } }); executor.submit(() -> { System.out.println("Locked: " + lock.isLocked()); System.out.println("Held by me: " + lock.isHeldByCurrentThread()); boolean locked = lock.tryLock(); System.out.println("Lock acquired: " + locked); }); ConcurrentUtils.stop(executor); }
46. FileAppenderTest#testMultipleAppenders()
View license@Test public void testMultipleAppenders() throws Exception { final ExecutorService pool = Executors.newFixedThreadPool(THREADS); final Exception[] error = new Exception[1]; final int count = 100; final Runnable runnable = new FileWriterRunnable(false, count, error); for (int i = 0; i < THREADS; ++i) { pool.execute(runnable); } pool.shutdown(); pool.awaitTermination(10, TimeUnit.SECONDS); if (error[0] != null) { throw error[0]; } verifyFile(THREADS * count); }
47. FileAppenderTest#testMultipleLockedAppenders()
View license@Test public void testMultipleLockedAppenders() throws Exception { final ExecutorService pool = Executors.newFixedThreadPool(THREADS); final Exception[] error = new Exception[1]; final int count = 100; final Runnable runnable = new FileWriterRunnable(true, count, error); for (int i = 0; i < THREADS; ++i) { pool.execute(runnable); } pool.shutdown(); pool.awaitTermination(10, TimeUnit.SECONDS); if (error[0] != null) { throw error[0]; } verifyFile(THREADS * count); }
48. TestMTStmtNamedWindowJoinUniqueView#testJoin()
View licensepublic void testJoin() throws Exception { ExecutorService es = Executors.newFixedThreadPool(10); List<MyRunnable> runnables = new ArrayList<MyRunnable>(); for (int i = 0; i < 6; i++) { runnables.add(new MyRunnable(service.getEPRuntime())); } for (Runnable toRun : runnables) { es.submit(toRun); } es.shutdown(); es.awaitTermination(20, TimeUnit.SECONDS); for (MyRunnable runnable : runnables) { assertNull(runnable.getException()); } }
49. TestMTStmtStatelessEnummethod#tryCount()
View licensepublic void tryCount(int numThreads, int numMessages, String epl, GeneratorIteratorCallback generatorIteratorCallback) throws Exception { ExecutorService threadPool = Executors.newFixedThreadPool(numThreads); EPStatement stmt = engine.getEPAdministrator().createEPL(epl); SupportUpdateListener listener = new SupportUpdateListener(); stmt.addListener(listener); Future future[] = new Future[numThreads]; for (int i = 0; i < numThreads; i++) { future[i] = threadPool.submit(new SendEventCallable(i, engine, new GeneratorIterator(numMessages, generatorIteratorCallback))); } threadPool.shutdown(); threadPool.awaitTermination(10, TimeUnit.SECONDS); for (int i = 0; i < numThreads; i++) { assertTrue((Boolean) future[i].get()); } assertEquals(numMessages * numThreads, listener.getNewDataListFlattened().length); }
50. SerialParallelLazyPerformanceTest#groupBy()
View licenseprivate void groupBy(UnifiedSet<String> words) { MutableList<Runnable> runnables = FastList.newList(); runnables.add(() -> this.basicSerialGroupByPerformance(words, SERIAL_RUN_COUNT)); int cores = Runtime.getRuntime().availableProcessors(); ExecutorService service = Executors.newFixedThreadPool(cores); runnables.add(() -> this.basicParallelLazyGroupByPerformance(words, "Lambda", ALPHAGRAM_LAMBDA, PARALLEL_RUN_COUNT, cores, service)); runnables.add(() -> this.basicParallelLazyGroupByPerformance(words, "Function", ALPHAGRAM_FUNCTION, PARALLEL_RUN_COUNT, cores, service)); runnables.add(() -> this.basicParallelLazyGroupByPerformance(words, "MethodRef", ALPHAGRAM_METHOD_REF, PARALLEL_RUN_COUNT, cores, service)); runnables.add(() -> this.basicParallelLazyJava8GroupByPerformance(words, "Lambda", JAVA_ALPHAGRAM_LAMBDA, PARALLEL_RUN_COUNT)); Set<String> hashSet = new HashSet<>(words); runnables.add(() -> this.basicParallelLazyJava8GroupByPerformance(hashSet, "Lambda", JAVA_ALPHAGRAM_LAMBDA, PARALLEL_RUN_COUNT)); runnables.add(() -> this.basicParallelLazyJava8GroupByPerformance(hashSet, "Function", JAVA_ALPHAGRAM_FUNCTION, PARALLEL_RUN_COUNT)); runnables.add(() -> this.basicParallelLazyJava8GroupByPerformance(hashSet, "MethodRef", JAVA_ALPHAGRAM_METHOD_REF, PARALLEL_RUN_COUNT)); this.shuffleAndRun(runnables); service.shutdown(); try { service.awaitTermination(1, TimeUnit.MINUTES); } catch (InterruptedException e) { throw new RuntimeException(e); } }
51. CreateTableIntegrationTest#executeCreateTableThreaded()
View licenseprivate void executeCreateTableThreaded(final String statement) throws Throwable { ExecutorService executorService = Executors.newFixedThreadPool(20); final AtomicReference<Throwable> lastThrowable = new AtomicReference<>(); for (int i = 0; i < 20; i++) { executorService.submit(new Runnable() { @Override public void run() { try { execute(statement); } catch (Throwable t) { lastThrowable.set(t); } } }); } executorService.shutdown(); assertThat("executorservice did not shutdown within timeout", executorService.awaitTermination(3, TimeUnit.SECONDS), is(true)); Throwable throwable = lastThrowable.get(); if (throwable != null) { throw throwable; } }
52. MoreExecutorsTest#testDirectExecutorService_awaitTermination_missedSignal()
View license/** * Test for a bug where threads weren't getting signaled when shutdown was called, only when * tasks completed. */ public void testDirectExecutorService_awaitTermination_missedSignal() { final ExecutorService service = MoreExecutors.newDirectExecutorService(); Thread waiter = new Thread() { @Override public void run() { try { service.awaitTermination(1, TimeUnit.DAYS); } catch (InterruptedException e) { return; } } }; waiter.start(); awaitTimedWaiting(waiter); service.shutdown(); Uninterruptibles.joinUninterruptibly(waiter, 10, TimeUnit.SECONDS); if (waiter.isAlive()) { waiter.interrupt(); fail("awaitTermination failed to trigger after shutdown()"); } }
53. SingleServiceIntegrationTest#testAsyncServiceBlockingLifecycle()
View licensepublic final void testAsyncServiceBlockingLifecycle() throws InterruptedException, ExecutionException, TimeoutException { ExecutorService executor = Executors.newSingleThreadExecutor(); final AtomicInteger integer = new AtomicInteger(2); AsyncService service = new AsyncService(executor) { @Override protected void onStart() { assertEquals(2, integer.getAndDecrement()); } @Override protected void onStop() { assertEquals(1, integer.getAndDecrement()); } }; service.start().get(2, TimeUnit.SECONDS); service.stop().get(2, TimeUnit.SECONDS); executor.shutdown(); assertEquals(0, integer.get()); }
54. TestDFSLocatedBlocks#testInsertRangeConcurrent()
View license@Test public void testInsertRangeConcurrent() throws Exception { ExecutorService exec = Executors.newFixedThreadPool(NUM_THREADS); for (int iteration = 0; iteration < NUM_ITERATIONS; ++iteration) { List<LocatedBlock> allBlocks = randomDFSLocatedBlocks(NUM_BLOCKS, 0).getLocatedBlocks(); DFSLocatedBlocks dfsLocatedBlocks = randomDFSLocatedBlocks(0, 0); List<Future<Boolean>> results = new ArrayList<Future<Boolean>>(); for (int iThread = 0; iThread < NUM_THREADS; ++iThread) { results.add(exec.submit(new InsertRangeThread(allBlocks, dfsLocatedBlocks))); } for (Future<Boolean> f : results) { assertTrue(f.get()); } LOG.info("# located blocks: " + dfsLocatedBlocks.getLocatedBlocks().size()); } exec.shutdown(); }
55. App#main()
View license/** * Program entry point * * @param args command line args */ public static void main(String[] args) { ExecutorService executeService = Executors.newFixedThreadPool(10); ReaderWriterLock lock = new ReaderWriterLock(); // Start 5 readers IntStream.range(0, 5).forEach( i -> executeService.submit(new Reader("Reader " + i, lock.readLock()))); // Start 5 writers IntStream.range(0, 5).forEach( i -> executeService.submit(new Writer("Writer " + i, lock.writeLock()))); // In the system console, it can see that the read operations are executed concurrently while // write operations are exclusive. executeService.shutdown(); try { executeService.awaitTermination(5, TimeUnit.SECONDS); } catch (InterruptedException e) { System.out.println("Error waiting for ExecutorService shutdown"); } }
56. SingletonTest#testMultipleCallsReturnTheSameObjectInDifferentThreads()
View license/** * Test singleton instance in a concurrent setting */ @Test(timeout = 10000) public void testMultipleCallsReturnTheSameObjectInDifferentThreads() throws Exception { // Create 10000 tasks and inside each callable instantiate the singleton class final List<Callable<S>> tasks = new ArrayList<>(); for (int i = 0; i < 10000; i++) { tasks.add(this.singletonInstanceMethod::get); } // Use up to 8 concurrent threads to handle the tasks final ExecutorService executorService = Executors.newFixedThreadPool(8); final List<Future<S>> results = executorService.invokeAll(tasks); // wait for all of the threads to complete final S expectedInstance = this.singletonInstanceMethod.get(); for (Future<S> res : results) { final S instance = res.get(); assertNotNull(instance); assertSame(expectedInstance, instance); } // tidy up the executor executorService.shutdown(); }
57. TaskTest#testIdGeneration()
View license/** * Verify if the generated id is unique for each task, even if the tasks are created in separate * threads */ @Test(timeout = 10000) public void testIdGeneration() throws Exception { final ExecutorService service = Executors.newFixedThreadPool(THREAD_COUNT); final List<Callable<Integer>> tasks = new ArrayList<>(); for (int i = 0; i < TASK_COUNT; i++) { tasks.add(() -> factory.apply(1).getId()); } final List<Integer> ids = service.invokeAll(tasks).stream().map(TaskTest::get).filter(Objects::nonNull).collect(Collectors.toList()); service.shutdownNow(); final long uniqueIdCount = ids.stream().distinct().count(); assertEquals(TASK_COUNT, ids.size()); assertEquals(TASK_COUNT, uniqueIdCount); }
58. Executors1#test1()
View licenseprivate static void test1(long seconds) { ExecutorService executor = Executors.newSingleThreadExecutor(); executor.submit(() -> { try { TimeUnit.SECONDS.sleep(seconds); String name = Thread.currentThread().getName(); System.out.println("task finished: " + name); } catch (InterruptedException e) { System.err.println("task interrupted"); } }); stop(executor); }
59. Executors2#test2()
View licenseprivate static void test2() throws InterruptedException, ExecutionException { ExecutorService executor = Executors.newFixedThreadPool(1); Future<Integer> future = executor.submit(() -> { try { TimeUnit.SECONDS.sleep(1); return 123; } catch (InterruptedException e) { throw new IllegalStateException("task interrupted", e); } }); executor.shutdownNow(); future.get(); }
60. Executors2#test1()
View licenseprivate static void test1() throws InterruptedException, ExecutionException { ExecutorService executor = Executors.newFixedThreadPool(1); Future<Integer> future = executor.submit(() -> { try { TimeUnit.SECONDS.sleep(1); return 123; } catch (InterruptedException e) { throw new IllegalStateException("task interrupted", e); } }); System.out.println("future done: " + future.isDone()); Integer result = future.get(); System.out.println("future done: " + future.isDone()); System.out.print("result: " + result); executor.shutdownNow(); }
61. Executors3#test5()
View licenseprivate static void test5() throws InterruptedException, ExecutionException { ExecutorService executor = Executors.newWorkStealingPool(); List<Callable<String>> callables = Arrays.asList(callable("task1", 2), callable("task2", 1), callable("task3", 3)); String result = executor.invokeAny(callables); System.out.println(result); executor.shutdown(); }
62. Executors3#test4()
View licenseprivate static void test4() throws InterruptedException { ExecutorService executor = Executors.newWorkStealingPool(); List<Callable<String>> callables = Arrays.asList(() -> "task1", () -> "task2", () -> "task3"); executor.invokeAll(callables).stream().map( future -> { try { return future.get(); } catch (Exception e) { throw new IllegalStateException(e); } }).forEach(System.out::println); executor.shutdown(); }
63. Lock6#main()
View licensepublic static void main(String[] args) { ExecutorService executor = Executors.newFixedThreadPool(2); StampedLock lock = new StampedLock(); executor.submit(() -> { long stamp = lock.readLock(); try { if (count == 0) { stamp = lock.tryConvertToWriteLock(stamp); if (stamp == 0L) { System.out.println("Could not convert to write lock"); stamp = lock.writeLock(); } count = 23; } System.out.println(count); } finally { lock.unlock(stamp); } }); ConcurrentUtils.stop(executor); }
64. DateConversionTest#test()
View license/** * Put it under a high-concurrency to make sure nothing bad happens. */ @Test public void test() throws Exception { final DateConverter dc = new DateConverter(); ExecutorService es = Executors.newFixedThreadPool(10); List<Future> futures = new ArrayList<Future>(); for (int i = 0; i < 10; i++) { futures.add(es.submit(new Callable<Object>() { public Object call() throws Exception { for (int i = 0; i < 10000; i++) dc.fromString("2008-08-26 15:40:14.568 GMT-03:00"); return null; } })); } for (Future f : futures) { f.get(); } es.shutdown(); }
65. WritePerformanceTest#testConcurrentWrite()
View license@Test @BenchmarkOptions(benchmarkRounds = 3, warmupRounds = 1) public void testConcurrentWrite() throws Exception { final AtomicInteger counter = new AtomicInteger(0); final byte[] data = new byte[100]; ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 2); for (int i = 0; i < inserts; i++) { executor.submit(new Runnable() { public void run() { try { journal.write(data, Journal.WriteType.ASYNC); counter.incrementAndGet(); } catch (Exception ex) { ex.printStackTrace(); } } }); } executor.shutdown(); assertTrue(executor.awaitTermination(1, TimeUnit.MINUTES)); assertEquals(inserts, counter.get()); }
66. TSpacePerformanceTest#testReadPerformance()
View license@Test public void testReadPerformance() throws Throwable { int size = 10; ExecutorService es = new ThreadPoolExecutor(size, Integer.MAX_VALUE, 30, TimeUnit.SECONDS, new SynchronousQueue()); ((ThreadPoolExecutor) es).prestartAllCoreThreads(); for (int i = 0; i < size; i++) es.execute(new WriteSpaceTask("PerformTask-" + i)); ISOUtil.sleep(500); printAvg(t1, "Avg. write: "); for (int i = 0; i < size; i++) es.execute(new ReadSpaceTask("PerformTask-" + i)); ISOUtil.sleep(500); es.shutdown(); printAvg(t2, "Avg. read : "); }
67. TSpacePerformanceTest#testStolenEntryAtNotify()
View license@Ignore("Remove it when TSpace can pass it") @Test public void testStolenEntryAtNotify() throws Throwable { int size = 10; final ExecutorService es = new ThreadPoolExecutor(size * 2, Integer.MAX_VALUE, 30, TimeUnit.SECONDS, new SynchronousQueue()); ((ThreadPoolExecutor) es).prestartAllCoreThreads(); for (int i = 0; i < size; i++) es.execute(new WriteSpaceWithNotifyReadTask("WriteTask-" + i)); //Threads which may stole entries for (int i = 0; i < size; i++) es.execute(new ReadSpaceTask("WriteTask-" + i)); assertNull("Detected stolen entry at notify", sp2.in("lost-entry", 200)); es.shutdownNow(); // es.awaitTermination(5, TimeUnit.SECONDS); }
68. AODETest#testTrainC_ClassificationDataSet_ExecutorService()
View license@Test public void testTrainC_ClassificationDataSet_ExecutorService() { System.out.println("trainC"); AODE instance = new AODE(); ExecutorService ex = Executors.newFixedThreadPool(SystemInfo.LogicalCores); ClassificationDataSet train = FixedProblems.getSimpleKClassLinear(10000, 3, new XOR96()); ClassificationDataSet test = FixedProblems.getSimpleKClassLinear(1000, 3, new XOR96()); ClassificationModelEvaluation cme = new ClassificationModelEvaluation(instance, train, ex); cme.setDataTransformProcess(new DataTransformProcess(new NumericalToHistogram.NumericalToHistogramTransformFactory())); cme.evaluateTestSet(test); assertTrue(cme.getErrorRate() <= 0.001); ex.shutdownNow(); }
69. MultivariateNormalsTest#testTrainC_ClassificationDataSet_ExecutorService()
View license@Test public void testTrainC_ClassificationDataSet_ExecutorService() { System.out.println("trainC"); MultivariateNormals instance = new MultivariateNormals(); ClassificationDataSet train = FixedProblems.getCircles(1000, 3, 0.1, 1.0, 10.0); ClassificationDataSet test = FixedProblems.getCircles(100, 3, 0.1, 1.0, 10.0); ExecutorService ex = Executors.newFixedThreadPool(SystemInfo.LogicalCores); ClassificationModelEvaluation cme = new ClassificationModelEvaluation(instance, train); cme.evaluateTestSet(test); assertTrue(cme.getErrorRate() <= 0.001); ex.shutdownNow(); }
70. AdaBoostM1PLTest#testTrainC_ClassificationDataSet_ExecutorService()
View license@Test public void testTrainC_ClassificationDataSet_ExecutorService() { System.out.println("trainC"); AdaBoostM1PL instance = new AdaBoostM1PL(new DecisionStump(), 50); ExecutorService ex = Executors.newFixedThreadPool(SystemInfo.LogicalCores); ClassificationDataSet train = FixedProblems.getCircles(1000, .1, 10.0); ClassificationDataSet test = FixedProblems.getCircles(100, .1, 10.0); ClassificationModelEvaluation cme = new ClassificationModelEvaluation(instance, train, ex); cme.evaluateTestSet(test); assertTrue(cme.getErrorRate() <= 0.15); ex.shutdownNow(); }
71. AdaBoostM1Test#testTrainC_ClassificationDataSet_ExecutorService()
View license@Test public void testTrainC_ClassificationDataSet_ExecutorService() { System.out.println("trainC"); AdaBoostM1 instance = new AdaBoostM1(new DecisionStump(), 50); ExecutorService ex = Executors.newFixedThreadPool(SystemInfo.LogicalCores); ClassificationDataSet train = FixedProblems.getCircles(1000, .1, 10.0); ClassificationDataSet test = FixedProblems.getCircles(100, .1, 10.0); ClassificationModelEvaluation cme = new ClassificationModelEvaluation(instance, train, ex); cme.evaluateTestSet(test); assertTrue(cme.getErrorRate() <= 0.15); ex.shutdownNow(); }
72. ArcX4Test#testTrainC_ClassificationDataSet_ExecutorService()
View license@Test public void testTrainC_ClassificationDataSet_ExecutorService() { System.out.println("trainC"); ArcX4 instance = new ArcX4(new DecisionStump(), 50); ExecutorService ex = Executors.newFixedThreadPool(SystemInfo.LogicalCores); ClassificationDataSet train = FixedProblems.getCircles(1000, .1, 10.0); ClassificationDataSet test = FixedProblems.getCircles(100, .1, 10.0); ClassificationModelEvaluation cme = new ClassificationModelEvaluation(instance, train, ex); cme.evaluateTestSet(test); assertTrue(cme.getErrorRate() <= 0.05); ex.shutdownNow(); }
73. BaggingTest#testTrainC_RegressionDataSet_ExecutorService()
View license@Test public void testTrainC_RegressionDataSet_ExecutorService() { System.out.println("train"); Bagging instance = new Bagging((Regressor) new DecisionTree()); ExecutorService ex = Executors.newFixedThreadPool(SystemInfo.LogicalCores); RegressionDataSet train = FixedProblems.getLinearRegression(1000, new XORWOW()); RegressionDataSet test = FixedProblems.getLinearRegression(100, new XORWOW()); RegressionModelEvaluation rme = new RegressionModelEvaluation(instance, train, ex); rme.evaluateTestSet(test); assertTrue(rme.getMeanError() <= test.getTargetValues().mean() * 0.5); ex.shutdownNow(); }
74. BaggingTest#testTrainC_ClassificationDataSet_ExecutorService()
View license@Test public void testTrainC_ClassificationDataSet_ExecutorService() { System.out.println("trainC"); Bagging instance = new Bagging((Classifier) new DecisionTree()); ExecutorService ex = Executors.newFixedThreadPool(SystemInfo.LogicalCores); ClassificationDataSet train = FixedProblems.getCircles(1000, .1, 10.0); ClassificationDataSet test = FixedProblems.getCircles(100, .1, 10.0); ClassificationModelEvaluation cme = new ClassificationModelEvaluation(instance, train, ex); cme.evaluateTestSet(test); assertTrue(cme.getErrorRate() <= 0.05); ex.shutdownNow(); }
75. EmphasisBoostTest#testTrainC_ClassificationDataSet_ExecutorService()
View license@Test public void testTrainC_ClassificationDataSet_ExecutorService() { System.out.println("trainC"); EmphasisBoost instance = new EmphasisBoost(new DecisionStump(), 50, 0.5); ExecutorService ex = Executors.newFixedThreadPool(SystemInfo.LogicalCores); ClassificationDataSet train = FixedProblems.getCircles(1000, .1, 10.0); ClassificationDataSet test = FixedProblems.getCircles(100, .1, 10.0); ClassificationModelEvaluation cme = new ClassificationModelEvaluation(instance, train, ex); cme.evaluateTestSet(test); assertTrue(cme.getErrorRate() <= 0.15); ex.shutdownNow(); }
76. LogitBoostPLTest#testTrainC_ClassificationDataSet_ExecutorService()
View license@Test public void testTrainC_ClassificationDataSet_ExecutorService() { System.out.println("trainC"); LogitBoostPL instance = new LogitBoostPL(new DecisionStump(), 50); ExecutorService ex = Executors.newFixedThreadPool(SystemInfo.LogicalCores); ClassificationDataSet train = FixedProblems.getCircles(1000, .1, 10.0); ClassificationDataSet test = FixedProblems.getCircles(100, .1, 10.0); ClassificationModelEvaluation cme = new ClassificationModelEvaluation(instance, train, ex); cme.evaluateTestSet(test); assertTrue(cme.getErrorRate() <= 0.15); ex.shutdownNow(); }
77. LogitBoostTest#testTrainC_ClassificationDataSet_ExecutorService()
View license@Test public void testTrainC_ClassificationDataSet_ExecutorService() { System.out.println("trainC"); LogitBoost instance = new LogitBoost(new DecisionStump(), 50); ExecutorService ex = Executors.newFixedThreadPool(SystemInfo.LogicalCores); ClassificationDataSet train = FixedProblems.getCircles(1000, .1, 10.0); ClassificationDataSet test = FixedProblems.getCircles(100, .1, 10.0); ClassificationModelEvaluation cme = new ClassificationModelEvaluation(instance, train, ex); cme.evaluateTestSet(test); assertTrue(cme.getErrorRate() <= 0.15); ex.shutdownNow(); }
78. ModestAdaBoostTest#testTrainC_ClassificationDataSet_ExecutorService()
View license@Test public void testTrainC_ClassificationDataSet_ExecutorService() { System.out.println("trainC"); ModestAdaBoost instance = new ModestAdaBoost(new DecisionStump(), 50); ExecutorService ex = Executors.newFixedThreadPool(SystemInfo.LogicalCores); ClassificationDataSet train = FixedProblems.getCircles(1000, .1, 10.0); ClassificationDataSet test = FixedProblems.getCircles(100, .1, 10.0); ClassificationModelEvaluation cme = new ClassificationModelEvaluation(instance, train, ex); cme.evaluateTestSet(test); assertTrue(cme.getErrorRate() <= 0.15); ex.shutdownNow(); }
79. SAMMETest#testTrainC_ClassificationDataSet_ExecutorService()
View license@Test public void testTrainC_ClassificationDataSet_ExecutorService() { System.out.println("trainC"); SAMME instance = new SAMME(new DecisionTree(2, 2, TreePruner.PruningMethod.NONE, 0.1), 50); ExecutorService ex = Executors.newFixedThreadPool(SystemInfo.LogicalCores); ClassificationDataSet train = FixedProblems.getCircles(1000, .1, 10.0, 100.0); ClassificationDataSet test = FixedProblems.getCircles(100, .1, 10.0, 100.0); ClassificationModelEvaluation cme = new ClassificationModelEvaluation(instance, train, ex); cme.evaluateTestSet(test); assertTrue(cme.getErrorRate() <= 0.15); ex.shutdownNow(); }
80. WaggingNormalTest#testTrainC_RegressionDataSet_ExecutorService()
View license@Test public void testTrainC_RegressionDataSet_ExecutorService() { System.out.println("train"); WaggingNormal instance = new WaggingNormal((Regressor) new DecisionTree(), 50); ExecutorService ex = Executors.newFixedThreadPool(SystemInfo.LogicalCores); RegressionDataSet train = FixedProblems.getLinearRegression(1000, new XORWOW()); RegressionDataSet test = FixedProblems.getLinearRegression(100, new XORWOW()); RegressionModelEvaluation rme = new RegressionModelEvaluation(instance, train, ex); rme.evaluateTestSet(test); assertTrue(rme.getMeanError() <= test.getTargetValues().mean() * 1.0); ex.shutdownNow(); }
81. WaggingNormalTest#testTrainC_ClassificationDataSet_ExecutorService()
View license@Test public void testTrainC_ClassificationDataSet_ExecutorService() { System.out.println("trainC"); WaggingNormal instance = new WaggingNormal((Classifier) new DecisionTree(), 50); ExecutorService ex = Executors.newFixedThreadPool(SystemInfo.LogicalCores); ClassificationDataSet train = FixedProblems.getCircles(1000, .1, 10.0); ClassificationDataSet test = FixedProblems.getCircles(100, .1, 10.0); ClassificationModelEvaluation cme = new ClassificationModelEvaluation(instance, train, ex); cme.evaluateTestSet(test); assertTrue(cme.getErrorRate() <= 0.05); ex.shutdownNow(); }
82. LWLTest#testTrainC_RegressionDataSet_ExecutorService()
View license@Test public void testTrainC_RegressionDataSet_ExecutorService() { System.out.println("train"); LWL instance = new LWL((Regressor) new DCDs(), 15, new EuclideanDistance()); ExecutorService ex = Executors.newFixedThreadPool(SystemInfo.LogicalCores); RegressionDataSet train = FixedProblems.getLinearRegression(1000, new XORWOW()); RegressionDataSet test = FixedProblems.getLinearRegression(100, new XORWOW()); RegressionModelEvaluation rme = new RegressionModelEvaluation(instance, train, ex); rme.evaluateTestSet(test); assertTrue(rme.getMeanError() <= test.getTargetValues().mean() * 0.25); ex.shutdownNow(); }
83. LWLTest#testTrainC_ClassificationDataSet_ExecutorService()
View license@Test public void testTrainC_ClassificationDataSet_ExecutorService() { System.out.println("trainC"); LWL instance = new LWL(new NaiveBayesUpdateable(), 15, new EuclideanDistance()); ExecutorService ex = Executors.newFixedThreadPool(SystemInfo.LogicalCores); ClassificationDataSet train = FixedProblems.getCircles(1000, 1.0, 10.0, 100.0); ClassificationDataSet test = FixedProblems.getCircles(100, 1.0, 10.0, 100.0); ClassificationModelEvaluation cme = new ClassificationModelEvaluation(instance, train, ex); cme.evaluateTestSet(test); assertTrue(cme.getErrorRate() <= 0.001); ex.shutdownNow(); }
84. ALMA2KTest#testTrainC_ClassificationDataSet_ExecutorService()
View license@Test public void testTrainC_ClassificationDataSet_ExecutorService() { System.out.println("trainC"); ALMA2K instance = new ALMA2K(new RBFKernel(0.5), 0.8); ExecutorService ex = Executors.newFixedThreadPool(SystemInfo.LogicalCores); ClassificationDataSet train = FixedProblems.getInnerOuterCircle(200, new XORWOW()); ClassificationDataSet test = FixedProblems.getInnerOuterCircle(100, new XORWOW()); ClassificationModelEvaluation cme = new ClassificationModelEvaluation(instance, train, ex); cme.evaluateTestSet(test); assertEquals(0, cme.getErrorRate(), 0.0); ex.shutdownNow(); }
85. CSKLRBatchTest#testTrainC_ClassificationDataSet_ExecutorService()
View license@Test public void testTrainC_ClassificationDataSet_ExecutorService() { System.out.println("trainC"); ExecutorService ex = Executors.newFixedThreadPool(SystemInfo.LogicalCores); for (CSKLR.UpdateMode mode : CSKLR.UpdateMode.values()) { CSKLRBatch instance = new CSKLRBatch(0.5, new RBFKernel(0.5), 10, mode, SupportVectorLearner.CacheMode.NONE); ClassificationDataSet train = FixedProblems.getInnerOuterCircle(200, new XORWOW()); ClassificationDataSet test = FixedProblems.getInnerOuterCircle(100, new XORWOW()); ClassificationModelEvaluation cme = new ClassificationModelEvaluation(instance, train, ex); cme.evaluateTestSet(test); assertEquals(0, cme.getErrorRate(), 0.0); } ex.shutdownNow(); }
86. CSKLRTest#testTrainC_ClassificationDataSet_ExecutorService()
View license@Test public void testTrainC_ClassificationDataSet_ExecutorService() { System.out.println("trainC"); ExecutorService ex = Executors.newFixedThreadPool(SystemInfo.LogicalCores); for (CSKLR.UpdateMode mode : CSKLR.UpdateMode.values()) { CSKLR instance = new CSKLR(0.5, new RBFKernel(0.5), 10, mode); instance.setMode(mode); ClassificationDataSet train = FixedProblems.getInnerOuterCircle(200, new XORWOW()); ClassificationDataSet test = FixedProblems.getInnerOuterCircle(100, new XORWOW()); ClassificationModelEvaluation cme = new ClassificationModelEvaluation(instance, train, ex); cme.evaluateTestSet(test); assertEquals(0, cme.getErrorRate(), 0.0); } ex.shutdownNow(); }
87. DUOLTest#testTrainC_ClassificationDataSet_ExecutorService()
View license@Test public void testTrainC_ClassificationDataSet_ExecutorService() { System.out.println("trainC"); DUOL instance = new DUOL(new RBFKernel(0.5)); ExecutorService ex = Executors.newFixedThreadPool(SystemInfo.LogicalCores); ClassificationDataSet train = FixedProblems.getInnerOuterCircle(200, new XORWOW()); ClassificationDataSet test = FixedProblems.getInnerOuterCircle(100, new XORWOW()); ClassificationModelEvaluation cme = new ClassificationModelEvaluation(instance, train, ex); cme.evaluateTestSet(test); assertEquals(0, cme.getErrorRate(), 0.0); ex.shutdownNow(); }
88. PerceptronTest#testTrainC_ClassificationDataSet_ExecutorService()
View license/** * Test of trainC method, of class Perceptron. */ @Test public void testTrainC_ClassificationDataSet_ExecutorService() { System.out.println("trainC"); ExecutorService threadPool = Executors.newFixedThreadPool(SystemInfo.LogicalCores); ClassificationDataSet train = FixedProblems.get2ClassLinear(200, new Random()); Perceptron instance = new Perceptron(); instance = instance.clone(); instance.trainC(train, threadPool); instance = instance.clone(); ClassificationDataSet test = FixedProblems.get2ClassLinear(200, new Random()); for (DataPointPair<Integer> dpp : test.getAsDPPList()) assertEquals(dpp.getPair().longValue(), instance.classify(dpp.getDataPoint()).mostLikely()); threadPool.shutdown(); }
89. RBFNetTest#testTrainC_ClassificationDataSet_ExecutorService()
View license/** * Test of trainC method, of class RBFNet. */ @Test public void testTrainC_ClassificationDataSet_ExecutorService() { System.out.println("trainC"); ClassificationDataSet trainSet = FixedProblems.getInnerOuterCircle(2000, new XOR96()); ClassificationDataSet testSet = FixedProblems.getInnerOuterCircle(200, new XOR96()); ExecutorService threadPool = Executors.newFixedThreadPool(SystemInfo.LogicalCores); for (RBFNet.Phase1Learner p1l : RBFNet.Phase1Learner.values()) for (RBFNet.Phase2Learner p2l : RBFNet.Phase2Learner.values()) { RBFNet net = new RBFNet(25).clone(); //CLOSEST_OPPOSITE_CENTROID needs a smaller value, shoudld be fine for others on this data set net.setAlpha(1); net.setPhase1Learner(p1l); net.setPhase2Learner(p2l); net.trainC(trainSet, threadPool); net = net.clone(); for (int i = 0; i < testSet.getSampleSize(); i++) assertEquals(testSet.getDataPointCategory(i), net.classify(testSet.getDataPoint(i)).mostLikely()); } threadPool.shutdown(); }
90. SOMTest#testTrainC_ClassificationDataSet_ExecutorService()
View license@Test public void testTrainC_ClassificationDataSet_ExecutorService() { System.out.println("trainC"); SOM instance = new SOM(5, 5); instance.setMaxIterations(200); ExecutorService ex = Executors.newFixedThreadPool(SystemInfo.LogicalCores); ClassificationDataSet train = FixedProblems.getCircles(1000, 1.0, 10.0, 100.0); ClassificationDataSet test = FixedProblems.getCircles(100, 1.0, 10.0, 100.0); ClassificationModelEvaluation cme = new ClassificationModelEvaluation(instance, train, ex); cme.evaluateTestSet(test); assertEquals(0, cme.getErrorRate(), 0.1); ex.shutdownNow(); }
91. PegasosTest#testTrainC_ClassificationDataSet_ExecutorService()
View license/** * Test of trainC method, of class Pegasos. */ @Test public void testTrainC_ClassificationDataSet_ExecutorService() { System.out.println("trainC"); ExecutorService threadPool = Executors.newFixedThreadPool(SystemInfo.LogicalCores); ClassificationDataSet train = FixedProblems.get2ClassLinear(200, new Random()); Pegasos instance = new Pegasos(); instance.trainC(train, threadPool); ClassificationDataSet test = FixedProblems.get2ClassLinear(200, new Random()); for (DataPointPair<Integer> dpp : test.getAsDPPList()) assertEquals(dpp.getPair().longValue(), instance.classify(dpp.getDataPoint()).mostLikely()); threadPool.shutdown(); }
92. KernelPCATest#testTrainC_ClassificationDataSet_ExecutorService()
View license@Test public void testTrainC_ClassificationDataSet_ExecutorService() { System.out.println("trainC"); ExecutorService ex = Executors.newFixedThreadPool(SystemInfo.LogicalCores); for (Nystrom.SamplingMethod sampMethod : Nystrom.SamplingMethod.values()) { DataModelPipeline instance = new DataModelPipeline((Classifier) new DCDs(), new KernelPCA.KernelPCATransformFactory(new RBFKernel(0.5), 20, 100, sampMethod)); ClassificationDataSet train = FixedProblems.getInnerOuterCircle(200, new XORWOW()); ClassificationDataSet test = FixedProblems.getInnerOuterCircle(100, new XORWOW()); ClassificationModelEvaluation cme = new ClassificationModelEvaluation(instance, train, ex); cme.evaluateTestSet(test); assertEquals(0, cme.getErrorRate(), 0.0); } ex.shutdownNow(); }
93. NystromTest#testTrainC_ClassificationDataSet_ExecutorService()
View license@Test public void testTrainC_ClassificationDataSet_ExecutorService() { System.out.println("trainC"); ExecutorService ex = Executors.newFixedThreadPool(SystemInfo.LogicalCores); for (Nystrom.SamplingMethod sampMethod : Nystrom.SamplingMethod.values()) { DataModelPipeline instance = new DataModelPipeline((Classifier) new DCDs(), new Nystrom.NystromTransformFactory(new RBFKernel(0.5), 250, sampMethod, 1e-5, false)); ClassificationDataSet train = FixedProblems.getInnerOuterCircle(400, new XORWOW()); ClassificationDataSet test = FixedProblems.getInnerOuterCircle(100, new XORWOW()); ClassificationModelEvaluation cme = new ClassificationModelEvaluation(instance, train, ex); cme.evaluateTestSet(test); assertEquals(0, cme.getErrorRate(), 0.0); } ex.shutdownNow(); }
94. RFF_RBFTest#testTrainC_ClassificationDataSet_ExecutorService()
View license@Test public void testTrainC_ClassificationDataSet_ExecutorService() { System.out.println("trainC"); ExecutorService ex = Executors.newFixedThreadPool(SystemInfo.LogicalCores); DataModelPipeline instance = new DataModelPipeline((Classifier) new DCDs(), new RFF_RBF.RFF_RBFTransformFactory(0.5, 100, true)); ClassificationDataSet train = FixedProblems.getInnerOuterCircle(200, new XORWOW()); ClassificationDataSet test = FixedProblems.getInnerOuterCircle(100, new XORWOW()); ClassificationModelEvaluation cme = new ClassificationModelEvaluation(instance, train, ex); cme.evaluateTestSet(test); assertEquals(0, cme.getErrorRate(), 0.0); ex.shutdownNow(); }
95. AveragedRegressorTest#testTrainC_RegressionDataSet_ExecutorService()
View license@Test public void testTrainC_RegressionDataSet_ExecutorService() { System.out.println("train"); AveragedRegressor instance = new AveragedRegressor(new KernelRLS(new LinearKernel(1), 1e-1), new KernelRLS(new LinearKernel(1), 1e-2), new KernelRLS(new LinearKernel(1), 1e-4)); ExecutorService ex = Executors.newFixedThreadPool(SystemInfo.LogicalCores); RegressionDataSet train = FixedProblems.getLinearRegression(500, new XORWOW()); RegressionDataSet test = FixedProblems.getLinearRegression(100, new XORWOW()); RegressionModelEvaluation rme = new RegressionModelEvaluation(instance, train, ex); rme.evaluateTestSet(test); assertTrue(rme.getMeanError() <= test.getTargetValues().mean() * 0.25); ex.shutdownNow(); }
96. KernelRidgeRegressionTest#testTrainC_RegressionDataSet_ExecutorService()
View license@Test public void testTrainC_RegressionDataSet_ExecutorService() { System.out.println("train"); KernelRidgeRegression instance = new KernelRidgeRegression(1e-1, new LinearKernel(1)); ExecutorService ex = Executors.newFixedThreadPool(SystemInfo.LogicalCores); RegressionDataSet train = FixedProblems.getLinearRegression(500, new XORWOW()); RegressionDataSet test = FixedProblems.getLinearRegression(100, new XORWOW()); RegressionModelEvaluation rme = new RegressionModelEvaluation(instance, train, ex); rme.evaluateTestSet(test); assertTrue(rme.getMeanError() <= test.getTargetValues().mean() * 0.25); ex.shutdownNow(); }
97. KernelRLSTest#testTrainC_RegressionDataSet_ExecutorService()
View license@Test public void testTrainC_RegressionDataSet_ExecutorService() { System.out.println("train"); KernelRLS instance = new KernelRLS(new LinearKernel(1), 1e-1); ExecutorService ex = Executors.newFixedThreadPool(SystemInfo.LogicalCores); RegressionDataSet train = FixedProblems.getLinearRegression(500, new XORWOW()); RegressionDataSet test = FixedProblems.getLinearRegression(100, new XORWOW()); RegressionModelEvaluation rme = new RegressionModelEvaluation(instance, train, ex); rme.evaluateTestSet(test); assertTrue(rme.getMeanError() <= test.getTargetValues().mean() * 0.25); ex.shutdownNow(); }
98. LogisticRegressionTest#testTrainC_ClassificationDataSet_ExecutorService()
View license@Test public void testTrainC_ClassificationDataSet_ExecutorService() { System.out.println("trainC"); LogisticRegression instance = new LogisticRegression(); ExecutorService ex = Executors.newFixedThreadPool(SystemInfo.LogicalCores); ClassificationDataSet train = FixedProblems.get2ClassLinear(100, new XORWOW()); ClassificationDataSet test = FixedProblems.get2ClassLinear(100, new XORWOW()); ClassificationModelEvaluation cme = new ClassificationModelEvaluation(instance, train, ex); cme.evaluateTestSet(test); assertTrue(cme.getErrorRate() <= 0.001); ex.shutdownNow(); }
99. ThreadsEngine#clearThreadPool()
View license/* * Stop */ public static void clearThreadPool() { for (ExecutorService pool : threadPool) { pool.shutdown(); } for (ExecutorService pool : threadPool) { try { pool.awaitTermination(10, TimeUnit.SECONDS); } catch (InterruptedException e) { e.printStackTrace(); } } threadPool.clear(); }
100. LibMatrixDNN#runParallelConvTask()
View licenseprivate static void runParallelConvTask(int constrainedNumThreads, int Z, TaskType type, ConvolutionParameters params) throws DMLRuntimeException { ArrayList<ConvTask> tasks = new ArrayList<ConvTask>(); // Static task allocation. TODO: Do this in dynamic way for (int n = 0; n < params.N; n++) { for (int z = 0; z < Z; z += TASK_SIZE) { tasks.add(new ConvTask(n, n + 1, z, Math.min(Z, z + TASK_SIZE), type, params)); } } ExecutorService pool = Executors.newFixedThreadPool(Math.min(constrainedNumThreads, tasks.size())); try { pool.invokeAll(tasks); } catch (InterruptedException e) { throw new DMLRuntimeException("Error while executing multi-threaded " + type.name(), e); } pool.shutdown(); }