Java Code Examples for java.util.concurrent.ExecutorService#shutdown()

The following examples show how to use java.util.concurrent.ExecutorService#shutdown() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IntegrationTestSendTraceRequests.java    From hbase with Apache License 2.0 6 votes vote down vote up
@Test
public void internalDoWork() throws Exception {
  util = createUtil();
  admin = util.getAdmin();
  setupReceiver();

  deleteTable();
  createTable();
  LinkedBlockingQueue<Long> rks = insertData();

  ExecutorService service = Executors.newFixedThreadPool(20);
  doScans(service, rks);
  doGets(service, rks);

  service.shutdown();
  service.awaitTermination(100, TimeUnit.SECONDS);
  Thread.sleep(90000);
  receiverHost.closeReceivers();
  util.restoreCluster();
  util = null;
}
 
Example 2
Source File: UniOnItemMapTest.java    From smallrye-mutiny with Apache License 2.0 6 votes vote down vote up
@Test
public void testThatMapperIsCalledOnTheRightExecutor() {
    UniAssertSubscriber<Integer> ts = new UniAssertSubscriber<>();
    ExecutorService executor = Executors.newSingleThreadExecutor();
    try {
        AtomicReference<String> threadName = new AtomicReference<>();
        Uni.createFrom().item(1)
                .emitOn(executor)
                .map(i -> {
                    threadName.set(Thread.currentThread().getName());
                    return i + 1;
                })
                .subscribe().withSubscriber(ts);

        ts.await().assertCompletedSuccessfully().assertItem(2);
        assertThat(threadName).isNotNull().doesNotHaveValue("main");
        assertThat(ts.getOnItemThreadName()).isEqualTo(threadName.get());
    } finally {
        executor.shutdown();
    }
}
 
Example 3
Source File: BlockingLimiterTest.java    From concurrency-limits with Apache License 2.0 6 votes vote down vote up
@Test
public void testMultipleBlockedThreads() throws InterruptedException, ExecutionException, TimeoutException {
    int numThreads = 8;
    SettableLimit limit = SettableLimit.startingAt(1);
    BlockingLimiter<Void> limiter = BlockingLimiter.wrap(SimpleLimiter.newBuilder().limit(limit).build());
    ExecutorService executorService = Executors.newFixedThreadPool(numThreads);
    try {
        for (Future<?> future : IntStream.range(0, numThreads)
                .mapToObj(x -> executorService.submit(() -> limiter.acquire(null).get().onSuccess()))
                .collect(Collectors.toList())) {
            future.get(1, TimeUnit.SECONDS);
        }
    } finally {
        executorService.shutdown();
    }
}
 
Example 4
Source File: ThreadUtils.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
/**
 * An implementation of the graceful stop sequence recommended by
 * {@link ExecutorService}.
 *
 * @param executor executor
 * @param timeout timeout
 * @param timeUnit timeUnit
 */
public static void shutdownGracefully(ExecutorService executor, long timeout, TimeUnit timeUnit) {
    // Disable new tasks from being submitted.
    executor.shutdown();
    try {
        // Wait a while for existing tasks to terminate.
        if (!executor.awaitTermination(timeout, timeUnit)) {
            executor.shutdownNow();
            // Wait a while for tasks to respond to being cancelled.
            if (!executor.awaitTermination(timeout, timeUnit)) {
                log.warn(String.format("%s didn't terminate!", executor));
            }
        }
    } catch (InterruptedException ie) {
        // (Re-)Cancel if current thread also interrupted.
        executor.shutdownNow();
        // Preserve interrupt status.
        Thread.currentThread().interrupt();
    }
}
 
Example 5
Source File: Command.java    From training with MIT License 6 votes vote down vote up
private static void stop(ExecutorService executor) {
	try {
	    System.out.println("attempt to shutdown executor");
	    executor.shutdown();
	    executor.awaitTermination(5, TimeUnit.SECONDS);
	}
	catch (InterruptedException e) {
	    System.err.println("tasks interrupted");
	}
	finally {
	    if (!executor.isTerminated()) {
	        System.err.println("cancel non-finished tasks");
	    }
	    executor.shutdownNow();
	    System.out.println("shutdown finished");
	}
}
 
Example 6
Source File: LotsOfCancels.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {

        // create a bunch of directories. Create two tasks for each directory,
        // one to bash on cancel, the other to poll the events
        ExecutorService pool = Executors.newCachedThreadPool();
        try {
            Path top = Files.createTempDirectory("work");
            top.toFile().deleteOnExit();
            for (int i=1; i<=16; i++) {
                Path dir = Files.createDirectory(top.resolve("dir-" + i));
                WatchService watcher = FileSystems.getDefault().newWatchService();
                pool.submit(() -> handle(dir, watcher));
                pool.submit(() -> poll(watcher));
            }
        } finally {
            pool.shutdown();
        }

        // give thread pool lots of time to terminate
        if (!pool.awaitTermination(5L, TimeUnit.MINUTES))
            throw new RuntimeException("Thread pool did not terminate");

        if (failed)
            throw new RuntimeException("Test failed, see log for details");
    }
 
Example 7
Source File: Crawler.java    From tools-journey with Apache License 2.0 6 votes vote down vote up
private void parallelDrainQueue(int threadCount) {
    ExecutorService executor = Executors.newFixedThreadPool(threadCount);
    for (int i = 0; i < threadCount; i++) {
        executor.execute(new NamedRunnable("Crawler %s", i) {
            @Override
            protected void execute() {
                try {
                    drainQueue();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    executor.shutdown();
}
 
Example 8
Source File: AbstractWriteBehindTestBase.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testBatchedWriteBehindBlocksWhenFull() throws Exception {
  final Semaphore gate = new Semaphore(0);
  CacheLoaderWriter<String, String> loaderWriter = mock(CacheLoaderWriter.class);
  doAnswer(invocation -> {
    gate.acquire();
    return null;
  }).when(loaderWriter).writeAll(any(Iterable.class));

  CacheLoaderWriterProvider cacheLoaderWriterProvider = getMockedCacheLoaderWriterProvider(loaderWriter);

  try (CacheManager cacheManager = managerBuilder().using(cacheLoaderWriterProvider).build(true)) {
    final Cache<String, String> testCache = cacheManager.createCache("testBatchedWriteBehindBlocksWhenFull", configurationBuilder()
      .withLoaderWriter(loaderWriter)
      .withService(newBatchedWriteBehindConfiguration(Long.MAX_VALUE, SECONDS, 1).queueSize(1).build())
      .build());

    testCache.put("key1", "value");
    testCache.put("key2", "value");

    ExecutorService executor = Executors.newSingleThreadExecutor();
    try {
      Future<?> blockedPut = executor.submit(() -> testCache.put("key3", "value"));

      try {
        blockedPut.get(100, MILLISECONDS);
        fail("Expected TimeoutException");
      } catch (TimeoutException e) {
        //expected
      }
      gate.release();
      blockedPut.get(10, SECONDS);
      gate.release(Integer.MAX_VALUE);
    } finally {
      executor.shutdown();
    }
  }
}
 
Example 9
Source File: ExecutorServiceRunner.java    From zerocode with Apache License 2.0 5 votes vote down vote up
public void runCallableFutures() {

        if (callables == null || callables.size() == 0) {
            throw new RuntimeException("No callable(s) was found to run. You can add one or more callables using 'addCallable(Callable callable)'");
        }

        ExecutorService executorService = newFixedThreadPool(numberOfThreads);

        try {
            executorService.invokeAll(callables).stream().forEach(future -> {
                for (int j = 0; j < numberOfThreads; j++) {
                    try {
                        LOGGER.info("Waiting in the transit for next test flight to adjust overall ramp up time, wait time now = " + delayBetweenTwoThreadsInMilliSecs);
                        sleep(delayBetweenTwoThreadsInMilliSecs.longValue());
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }

                    LOGGER.info(Thread.currentThread().getName() + " Future execution- Start.... Time = " + now());

                    execute(future);

                    LOGGER.info(Thread.currentThread().getName() + " Future execution- *Finished Time = " + now());
                }
            });
        } catch (InterruptedException interruptEx) {
            throw new RuntimeException(interruptEx);
        } finally {
            executorService.shutdown();
            while (!executorService.isTerminated()) {
                // wait for all tasks to finish executing
                // LOGGER.info("Still waiting for all threads to complete execution...");
            }
            LOGGER.warn("* Completed executing all virtual-user scenarios! *");
        }


    }
 
Example 10
Source File: ClientSocket.java    From javabase with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    ExecutorService pool = Executors.newFixedThreadPool(clientSize);
    for (int i = 0; i < clientSize; i++) {
        pool.execute(clientThread(i));
    }
pool.shutdown();
}
 
Example 11
Source File: EvaluationUtils.java    From AILibs with GNU Affero General Public License v3.0 5 votes vote down vote up
public static double performKernelLDA(final Instances instances, final int numThreads) throws Exception {

		logger.debug("Starting kernelized LDA evaluation...");

		List<Instances> split = WekaUtil.getStratifiedSplit(instances, 42, kernelSplitPortion);

		ExecutorService execService = Executors.newFixedThreadPool(numThreads);

		List<Future<Double>> futures = new ArrayList<>();
		Future<Double> result0 = execService.submit(() -> performLDA(new Instances(split.get(0))));
		futures.add(result0);

		for (final Map.Entry<Kernel, Instances> entry : getKernelsWithInstances(split.get(0))) {
			if (Thread.currentThread().isInterrupted()) {
				throw new InterruptedException(EVALUATION_STOPPED_MESSAGE);
			}

			Future<Double> result = execService.submit(() -> {
				Kernel kernel = entry.getKey();
				Instances insts = entry.getValue();

				Nystroem kernelFilter = new Nystroem();
				kernelFilter.setInputFormat(insts);
				kernelFilter.setKernel(kernel);

				insts = Filter.useFilter(insts, kernelFilter);

				return performLDA(insts);
			});
			futures.add(result);
		}

		execService.shutdown();
		execService.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);

		return evaluateFutures(futures);
	}
 
Example 12
Source File: StrongServerStoreProxyTest.java    From ehcache3 with Apache License 2.0 4 votes vote down vote up
@Test
public void testConcurrentAllInvalidationListener() throws Exception {
  SimpleClusterTierClientEntity clientEntity1 = createClientEntity("testConcurrentAllInvalidationListener", Consistency.STRONG, true);
  SimpleClusterTierClientEntity clientEntity2 = createClientEntity("testConcurrentAllInvalidationListener", Consistency.STRONG, false);

  final AtomicBoolean invalidating = new AtomicBoolean();
  final CountDownLatch latch = new CountDownLatch(2);

  StrongServerStoreProxy serverStoreProxy1 = new StrongServerStoreProxy("testConcurrentAllInvalidationListener", clientEntity1, mock(ServerCallback.class));
  StrongServerStoreProxy serverStoreProxy2 = new StrongServerStoreProxy("testConcurrentAllInvalidationListener", clientEntity2, new ServerCallback() {
    @Override
    public void onInvalidateHash(long hash, Chain evictedChain) {
      throw new AssertionError("Should not be called");
    }

    @Override
    public void onInvalidateAll() {
      if (!invalidating.compareAndSet(false, true)) {
        fail("Both threads entered the listener concurrently");
      }
      try {
        Thread.sleep(100);
      } catch (InterruptedException ie) {
        throw new AssertionError(ie);
      }
      invalidating.set(false);
      latch.countDown();
    }

    @Override
    public void onAppend(Chain beforeAppend, ByteBuffer appended) {
      fail("should not be called");
    }

    @Override
    public void compact(ServerStoreProxy.ChainEntry chain) {
      throw new AssertionError();
    }
  });

  ExecutorService executor = Executors.newCachedThreadPool();
  try {
    executor.submit(() -> {
      serverStoreProxy1.clear();
      return null;
    });
    executor.submit(() -> {
      serverStoreProxy1.clear();
      return null;
    });

    if (!latch.await(5, TimeUnit.SECONDS)) {
      fail("Both listeners were not called");
    }
  } finally {
    executor.shutdown();
  }
}
 
Example 13
Source File: MissingNotificationTest.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    System.out.println(
        ">>> Test for missing notifications.");

    System.out.println(">>> Create a Timer object.");
    final Timer timer = new Timer();

    timer.start();

    NotifListener listener = new NotifListener();
    timer.addNotificationListener(listener, null, null);

    ExecutorService executor = Executors.newFixedThreadPool(100);
    final Random rand = new Random();


    for (int i = 0; i < TASK_COUNT; i++) {
        executor.execute(new Runnable() {
            public void run() {
                long dateMillis = System.currentTimeMillis() + fixedDelay + rand.nextInt(2000);
                Date date = new Date(dateMillis);
                timer.addNotification("type", "msg", "userData", date);
            }
        });

    }

    executor.shutdown();
    executor.awaitTermination(20, TimeUnit.SECONDS);

    waitForNotificationsToEnd(listener);

    timer.stop();

    if (listener.count < TASK_COUNT) {
        throw new RuntimeException("Not fired: " + (TASK_COUNT - listener.count));
    } else {
        System.out.println(">>> All notifications handled OK");
    }

    System.out.println(">>> Bye bye!");
}
 
Example 14
Source File: MultithreadedReader.java    From citygml4j with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
	SimpleDateFormat df = new SimpleDateFormat("[HH:mm:ss] "); 

	System.out.println(df.format(new Date()) + "setting up citygml4j context and CityGML builder");
	CityGMLContext ctx = CityGMLContext.getInstance();
	CityGMLBuilder builder = ctx.createCityGMLBuilder();
	
	// create a fixed thread pool
	int nThreads = Runtime.getRuntime().availableProcessors() * 2;
	System.out.println(df.format(new Date()) + "setting up thread pool with " + nThreads + " threads");
	ExecutorService service = Executors.newFixedThreadPool(nThreads);
	
	System.out.println(df.format(new Date()) + "reading LOD3_Railway_v200.gml in a multithreaded fashion");
	
	// create a validating reader that chunks the input file on a per feature level
	CityGMLInputFactory in = builder.createCityGMLInputFactory();
	in.setProperty(CityGMLInputFactory.FEATURE_READ_MODE, FeatureReadMode.SPLIT_PER_FEATURE);
	in.setProperty(CityGMLInputFactory.USE_VALIDATION, true);
	
	CityGMLReader reader = in.createCityGMLReader(new File("datasets/LOD3_Railway_v200.gml"));
	
	while (reader.hasNext()) {
		// whereas the nextFeature() method of a CityGML reader completely unmarshals the 
		// XML chunk to an instance of the citygml4j object model and optionally validates
		// it before returning, the nextChunk() method returns faster but only provides a
		// set of SAX events.		
		final XMLChunk chunk = reader.nextChunk();
		
		// we forward every XML chunk to a separate thread of the thread pool
		// for unmarshalling and validation
		service.execute(() -> {
               try {
                   chunk.unmarshal();
               } catch (UnmarshalException | MissingADESchemaException e) {
                   //
               }

               // invoking the hasPassedXMLValidation() method prior to unmarshal()
               // or when using a non-validating CityGML reader will always yield false.
               boolean isValid = chunk.hasPassedXMLValidation();

               System.out.println("Thread '" + Thread.currentThread().getName() + "' unmarshalled " + chunk.getCityGMLClass() + "; valid: " + isValid);
           });
	}
	
	System.out.println(df.format(new Date()) + "shutting down threadpool");
	service.shutdown();
	service.awaitTermination(120, TimeUnit.SECONDS);
	
	reader.close();
	System.out.println(df.format(new Date()) + "sample citygml4j application successfully finished");
}
 
Example 15
Source File: CoopLockLoadIntegrationTest.java    From hadoop-connectors with Apache License 2.0 4 votes vote down vote up
@Test
public void moveDirectory_loadTest() throws Exception {
  GoogleCloudStorageFileSystemOptions gcsFsOptions =
      newGcsFsOptions().toBuilder()
          .setCloudStorageOptions(gcsOptions.toBuilder().setMaxHttpRequestRetries(0).build())
          .build();
  GoogleCloudStorageFileSystem gcsFs = newGcsFs(gcsFsOptions, httpRequestInitializer);

  String bucketName = gcsfsIHelper.createUniqueBucket("coop-load");
  URI bucketUri = new URI("gs://" + bucketName + "/");
  String dirName = "rename_" + UUID.randomUUID();
  String fileNamePrefix = "file_";
  URI srcFileUri = bucketUri.resolve(dirName + "_src");
  URI dstFileUri = bucketUri.resolve(dirName + "_dst");
  URI srcDirUri = bucketUri.resolve(dirName + "_src/");
  URI dstDirUri = bucketUri.resolve(dirName + "_dst/");

  int iterations = 10;

  // create file to rename
  for (int i = 0; i < iterations; i++) {
    gcsfsIHelper.writeTextFile(
        bucketName, srcDirUri.resolve(fileNamePrefix + i).getPath(), "file_content_" + i);
  }

  ExecutorService moveExecutor = Executors.newFixedThreadPool(iterations * 2);
  List<Future<?>> futures = new ArrayList<>(iterations * 2);
  for (int i = 0; i < iterations; i++) {
    URI srcUri1 = i % 4 == 0 ? srcFileUri : srcDirUri;
    URI dstUri1 = i % 4 == 1 ? dstFileUri : dstDirUri;
    futures.add(moveExecutor.submit(() -> renameUnchecked(gcsFs, srcUri1, dstUri1)));

    URI srcUri2 = i % 4 == 3 ? srcFileUri : srcDirUri;
    URI dstUri2 = i % 4 == 2 ? dstFileUri : dstDirUri;
    futures.add(moveExecutor.submit(() -> renameUnchecked(gcsFs, dstUri2, srcUri2)));
  }
  moveExecutor.shutdown();
  moveExecutor.awaitTermination(6, TimeUnit.MINUTES);
  assertWithMessage("Cooperative locking load test timed out")
      .that(moveExecutor.isTerminated())
      .isTrue();

  for (Future<?> f : futures) {
    f.get();
  }

  assertThat(gcsFs.exists(srcDirUri)).isTrue();
  assertThat(gcsFs.exists(dstDirUri)).isFalse();
  assertThat(gcsFs.listFileInfo(srcDirUri)).hasSize(iterations);
  for (int i = 0; i < iterations; i++) {
    assertThat(gcsFs.exists(srcDirUri.resolve(fileNamePrefix + i))).isTrue();
  }

  // Validate lock files
  List<URI> lockFiles =
      gcsFs.listFileInfo(bucketUri.resolve(LOCK_DIRECTORY)).stream()
          .map(FileInfo::getPath)
          .collect(toList());

  assertThat(lockFiles).hasSize(iterations * 4);
}
 
Example 16
Source File: RsaCertificateAuthorityClient.java    From protect with MIT License 4 votes vote down vote up
/**
 * Interacts with the servers to store an RSA sharing to a given secret
 * 
 * @param rsaSharing
 * @return
 * @throws ResourceUnavailableException
 * @throws BelowThresholdException
 */
private Boolean storeRsaSharing(final RsaSharing rsaSharing)
		throws ResourceUnavailableException, BelowThresholdException {

	// Server configuration
	final int numShareholders = this.serverConfiguration.getNumServers();
	final int reconstructionThreshold = this.serverConfiguration.getReconstructionThreshold();

	// We create a thread pool with a thread for each task and remote server
	final ExecutorService executor = Executors.newFixedThreadPool(numShareholders - 1);

	// The countdown latch tracks progress towards reaching a threshold
	final CountDownLatch latch = new CountDownLatch(reconstructionThreshold);
	final AtomicInteger failureCounter = new AtomicInteger(0);
	final int maximumFailures = (numShareholders - reconstructionThreshold);

	// Each task deposits its result into this map after verifying it is correct and
	// consistent
	// TODO: Add verification via proofs
	final List<Object> successfulResults = Collections.synchronizedList(new ArrayList<>());

	// Collect pulic data to register with servers

	// Send the public exponenet to the server
	final BigInteger exponent = rsaSharing.getPublicKey().getPublicExponent();

	// Send the modulus to the server
	final BigInteger modulus = rsaSharing.getPublicKey().getModulus();

	// Send the generator to the server
	final BigInteger v = rsaSharing.getV();

	StringBuilder allVerificationKeys = new StringBuilder();
	for (int i = 1; i <= this.serverConfiguration.getNumServers(); i++) {
		allVerificationKeys.append("&v_" + i + "=" + rsaSharing.getVerificationKeys()[i - 1]);
	}

	// Create a partial result task for everyone except ourselves
	int serverId = 0;
	for (final InetSocketAddress serverAddress : this.serverConfiguration.getServerAddresses()) {
		serverId++;
		final String serverIp = serverAddress.getAddress().getHostAddress();
		final int serverPort = CommonConfiguration.BASE_HTTP_PORT + serverId;

		// Send share to the server
		final BigInteger share = rsaSharing.getShares()[serverId - 1].getY();

		final String linkUrl = "https://" + serverIp + ":" + serverPort + "/store?secretName=" + this.secretName
				+ "&e=" + exponent + "&n=" + modulus + "&v=" + v + allVerificationKeys.toString() + "&share="
				+ share;

		// Create new task to get the partial exponentiation result from the server
		executor.submit(new PartialResultTask(this, serverId, linkUrl, successfulResults, latch, failureCounter,
				maximumFailures) {
			@Override
			protected void parseJsonResult(final String json) throws Exception {

				// Store result for later processing
				successfulResults.add(Boolean.TRUE);

				// Everything checked out, increment successes
				latch.countDown();

			}
		});
	}

	try {
		// Once we have K successful responses we can interpolate our share
		latch.await();

		// Check that we have enough results to interpolate the share
		if (failureCounter.get() <= maximumFailures) {

			// When complete, interpolate the result at zero (where the secret lies)
			final Boolean wereSuccessful = (Boolean) getConsistentConfiguration(successfulResults,
					reconstructionThreshold);
			executor.shutdown();

			return wereSuccessful;
		} else {
			executor.shutdown();
			throw new ResourceUnavailableException();
		}
	} catch (InterruptedException e) {
		throw new RuntimeException(e);
	}
}
 
Example 17
Source File: DomainGracefulShutdownTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Test
public void testGracefulShutdownDomainLevel() throws Exception {
    DomainClient client = domainMasterLifecycleUtil.getDomainClient();

    final String address = "http://" + TestSuiteEnvironment.getServerAddress() + ":8080/web-suspend";
    ExecutorService executorService = Executors.newSingleThreadExecutor();
    try {
        Future<Object> result = executorService.submit(new Callable<Object>() {
            @Override
            public Object call() throws Exception {
                return HttpRequest.get(address, 60, TimeUnit.SECONDS);
            }
        });

        Thread.sleep(1000); //nasty, but we need to make sure the HTTP request has started

        ModelNode op = new ModelNode();
        op.get(ModelDescriptionConstants.OP).set("stop-servers");
        op.get(ModelDescriptionConstants.TIMEOUT).set(60);
        op.get(ModelDescriptionConstants.BLOCKING).set(false);
        client.execute(op);

        //make sure requests are being rejected
        final HttpURLConnection conn = (HttpURLConnection) new URL(address).openConnection();
        try {
            conn.setDoInput(true);
            int responseCode = conn.getResponseCode();
            Assert.assertEquals(503, responseCode);
        } finally {
            conn.disconnect();
        }

        //make sure the server is still up, and trigger the actual shutdown
        HttpRequest.get(address + "?" + TestUndertowService.SKIP_GRACEFUL + "=true", 10, TimeUnit.SECONDS);
        Assert.assertEquals(SuspendResumeHandler.TEXT, result.get());

        //make sure our initial request completed
        Assert.assertEquals(SuspendResumeHandler.TEXT, result.get());


    } finally {
        executorService.shutdown();
    }
}
 
Example 18
Source File: Slicer.java    From dawnsci with Eclipse Public License 1.0 4 votes vote down vote up
public static void visitParallel(ISliceViewIterator iterator, final SliceVisitor visitor, int nProcessors) throws Exception {

		//Can't just farm out each slice to a separate thread, need to block when thread pool full,
		//other wise there is the potential run out of memory from loading all the data before any is processed
		
		//use one less thread than processors, as we are using one for the rejectedhandler
		nProcessors = nProcessors - 1;

		BlockingQueue<Runnable> blockingQueue = new ArrayBlockingQueue<Runnable>(nProcessors);
	    RejectedExecutionHandler rejectedExecutionHandler = new ThreadPoolExecutor.CallerRunsPolicy();
	    final ExecutorService pool =  new ThreadPoolExecutor(nProcessors, nProcessors, 
	        0L, TimeUnit.MILLISECONDS, blockingQueue, rejectedExecutionHandler);
	    
		final SliceVisitor parallel = new SliceVisitor() {

			@Override
			public void visit(final IDataset slice) throws Exception {
				
				pool.execute(new Runnable() {
					
					@Override
					public void run() {
						try {
							
						    visitor.visit(slice);
						    
						    
						} catch (Throwable ne) {
							ne.printStackTrace();
							// TODO Fix me - should runtime exception really be thrown back to Fork/Join?
							//throw new RuntimeException(ne.getMessage(), ne);
						}
					}
				});
			}

			@Override
			public boolean isCancelled() {
				return visitor.isCancelled();
			}
		};
		
		Slicer.visit(iterator, parallel);
		
		pool.shutdown();
		
		while (!pool.awaitTermination(200, TimeUnit.MILLISECONDS)){
			if (visitor.isCancelled()) break;
		}

	}
 
Example 19
Source File: AbstractConcurrentTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
private void shutdownExecutorService(final ExecutorService executorService) throws InterruptedException {
    executorService.shutdown();
    if (!executorService.awaitTermination(5, TimeUnit.SECONDS)) {
        executorService.shutdownNow();
    }
}
 
Example 20
Source File: ObjectPoolIssue326.java    From commons-pool with Apache License 2.0 4 votes vote down vote up
private void run() throws Exception {
    final GenericKeyedObjectPoolConfig poolConfig = new GenericKeyedObjectPoolConfig();
    poolConfig.setMaxTotal(10);
    poolConfig.setMaxTotalPerKey(5);
    poolConfig.setMinIdlePerKey(-1);
    poolConfig.setMaxIdlePerKey(-1);
    poolConfig.setLifo(true);
    poolConfig.setFairness(true);
    poolConfig.setMaxWaitMillis(30 * 1000);
    poolConfig.setMinEvictableIdleTimeMillis(-1);
    poolConfig.setSoftMinEvictableIdleTimeMillis(-1);
    poolConfig.setNumTestsPerEvictionRun(1);
    poolConfig.setTestOnCreate(false);
    poolConfig.setTestOnBorrow(false);
    poolConfig.setTestOnReturn(false);
    poolConfig.setTestWhileIdle(false);
    poolConfig.setTimeBetweenEvictionRunsMillis(5 * 1000);
    poolConfig.setEvictionPolicyClassName(BaseObjectPoolConfig.DEFAULT_EVICTION_POLICY_CLASS_NAME);
    poolConfig.setBlockWhenExhausted(false);
    poolConfig.setJmxEnabled(false);
    poolConfig.setJmxNameBase(null);
    poolConfig.setJmxNamePrefix(null);

    final GenericKeyedObjectPool<Integer, Object> pool = new GenericKeyedObjectPool<>(new ObjectFactory(), poolConfig);

    // number of threads to reproduce is finicky. this count seems to be best for my
    // 4 core box.
    // too many doesn't reproduce it ever, too few doesn't either.
    final ExecutorService service = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 2);
    final long startTime = System.currentTimeMillis();
    long testIter = 0;
    try {
        while (true) {
            testIter++;
            if (testIter % 1000 == 0) {
                System.out.println(testIter);
            }
            final List<Task> tasks = createTasks(pool);
            final List<Future<Object>> futures = service.invokeAll(tasks);
            for (final Future<Object> future : futures) {
                future.get();
            }
        }
    } finally {
        System.out.println("Time: " + (System.currentTimeMillis() - startTime) / 1000.0);
        service.shutdown();
    }
}