Java Code Examples for java.util.concurrent.Executors#newWorkStealingPool()

The following examples show how to use java.util.concurrent.Executors#newWorkStealingPool() . 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: AssemblyLine.java    From Java-Coding-Problems with MIT License 6 votes vote down vote up
private static void startConsumers() {

        logger.info(() -> "We have a consumers team of "
                + PROCESSORS + " members ...");

        consumerService = Executors.newWorkStealingPool();
        // consumerService = Executors.newCachedThreadPool();
        // consumerService = Executors.newWorkStealingPool(PROCESSORS);
        // consumerService = Executors.newFixedThreadPool(PROCESSORS);
        int queueSize = queue.size();

        startTime = System.currentTimeMillis();
        for (int i = 0; i < queueSize; i++) {    
            consumerService.execute(consumer);
        }

        consumerService.shutdown();
        try {
            consumerService.awaitTermination(Integer.MAX_VALUE, TimeUnit.MILLISECONDS);
        } catch (InterruptedException ex) {
            Logger.getLogger(AssemblyLine.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
 
Example 2
Source File: ClusterTest.java    From rapid with Apache License 2.0 6 votes vote down vote up
/**
 * Fail a set of nodes in a cluster by calling shutdown().
 *
 * @param nodesToFail list of Endpoint objects representing the nodes to fail
 */
private void failSomeNodes(final List<Endpoint> nodesToFail) {
    final ExecutorService executor = Executors.newWorkStealingPool(nodesToFail.size());
    try {
        final CountDownLatch latch = new CountDownLatch(nodesToFail.size());
        for (final Endpoint nodeToFail : nodesToFail) {
            executor.execute(() -> {
                try {
                    assertTrue(nodeToFail + " not in instances", instances.containsKey(nodeToFail));
                    instances.get(nodeToFail).shutdown();
                    instances.remove(nodeToFail);
                } finally {
                    latch.countDown();
                }
            });
        }
        latch.await();
    } catch (final InterruptedException e) {
        e.printStackTrace();
        fail();
    } finally {
        executor.shutdown();
    }
}
 
Example 3
Source File: MCROAISearchManager.java    From mycore with GNU General Public License v3.0 6 votes vote down vote up
public MCROAISearchManager() {
    this.resultMap = new ConcurrentHashMap<>();
    TimerTask tt = new TimerTask() {
        @Override
        public void run() {
            for (Map.Entry<String, MCROAISearcher> entry : resultMap.entrySet()) {
                String searchId = entry.getKey();
                MCROAISearcher searcher = entry.getValue();
                if ((searcher != null) && searcher.isExpired()) {
                    LOGGER.info("Removing expired resumption token {}", searchId);
                    resultMap.remove(searchId);
                }
            }
        }
    };
    new Timer().schedule(tt, new Date(System.currentTimeMillis() + MAX_AGE), MAX_AGE);
    runListRecordsParallel = MCRConfiguration2
        .getOrThrow(MCROAIAdapter.PREFIX + "RunListRecordsParallel", Boolean::parseBoolean);
    if (runListRecordsParallel) {
        executorService = Executors.newWorkStealingPool();
        MCRShutdownHandler.getInstance().addCloseable(executorService::shutdownNow);
    }
}
 
Example 4
Source File: Expedition.java    From timbuctoo with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Gather ResourceSync Framework documents from a source in ResultIndexes.
 *
 * @param url        the starting url to explore
 * @param authString authorization token, optional
 * @return List of resultIndexes of the exploration
 * @throws URISyntaxException   if the url could not be converted to a URI.
 * @throws InterruptedException at Executor interrupts.
 */
public List<ResultIndex> explore(String url, String authString) throws URISyntaxException, InterruptedException {
  URI uri = new URI(url);

  ExecutorService executor = Executors.newWorkStealingPool();

  List<Callable<ResultIndex>> callables = new ArrayList<>();
  callables.add(() -> exploreWellKnown(uri, authString));
  callables.add(() -> exploreLinks(uri));
  callables.add(() -> exploreRobotsTxt(uri));
  callables.add(() -> exploreRsDocumentUri(uri));

  return executor.invokeAll(callables)
    .stream()
    .map(future -> {
      try {
        return future.get();
      } catch (Exception e) {
        throw new IllegalStateException(e);
      }
    })
    .collect(Collectors.toList());
}
 
Example 5
Source File: LocalSynchronousTransformClientIntegrationTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void testParallelTransforms() throws Exception
{
    Collection<Callable<Void>> transforms = new ArrayList<>();
    ExecutorService executorService = Executors.newWorkStealingPool(10);
    for (int i=0; i<50; i++)
    {
        Callable<Void> callable = () ->
        {
            checkTransform("quick.txt", "text/plain", Collections.emptyMap(), true);
            return null;
        };
        transforms.add(callable);
    }
    executorService.invokeAll(transforms);
}
 
Example 6
Source File: Executors3.java    From java8-tutorial with MIT License 6 votes vote down vote up
private 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();
}
 
Example 7
Source File: CrfFeatureValueExpectationByModel.java    From CRF with MIT License 5 votes vote down vote up
public void calculate()
{
	featureValueExpectation = new BigDecimal[model.getFeatures().getFilteredFeatures().length];
	for (int i=0;i<featureValueExpectation.length;++i) {featureValueExpectation[i]=BigDecimal.ZERO;} // Explicit initialization to zero, just to be on the safe side.
	
	ExecutorService executor = Executors.newWorkStealingPool();
	List<Future<?>> futures = new LinkedList<>();
	
	while (corpusIterator.hasNext())
	{
		final List<? extends TaggedToken<K, G>> sentence = corpusIterator.next();
		futures.add(executor.submit(
				new Runnable()
				{
					@Override
					public void run()
					{
						addValueForSentence(sentence);
					}
				}));
	}
	for (Future<?> future: futures)
	{
		try
		{
			future.get();
		}
		catch (InterruptedException | ExecutionException e)
		{
			throw new CrfException(e);
		}
	}
}
 
Example 8
Source File: ThreadSafeConnection.java    From PGM with GNU Affero General Public License v3.0 5 votes vote down vote up
public ThreadSafeConnection(Supplier<Connection> connectionSupplier, int maxConnections)
    throws SQLException {
  this.connectionSupplier = connectionSupplier;
  this.connectionQueue = new LinkedBlockingQueue<>(maxConnections);
  this.executorService = Executors.newWorkStealingPool(maxConnections);

  releaseConnection(newConnection());
}
 
Example 9
Source File: ServiceBrokerConfig.java    From moleculer-java with MIT License 5 votes vote down vote up
public ServiceBrokerConfig(String nodeID, Cacher cacher, Transporter transporter) {

		// Create default thread pools
		int threads = Math.max(4, Runtime.getRuntime().availableProcessors());
		executor = Executors.newWorkStealingPool(threads);
		scheduler = Executors.newScheduledThreadPool(threads);

		// Set the default System Monitor
		monitor = defaultMonitor;

		// Set the default (generated) NodeID
		if (nodeID == null || nodeID.isEmpty()) {
			this.nodeID = getHostName() + '-' + monitor.getPID();
			long index = instanceCounter.incrementAndGet();
			if (index > 1) {
				this.nodeID += '-' + Long.toString(index);
			}
		} else {
			this.nodeID = nodeID;
		}

		// Set cacher
		if (cacher != null) {
			setCacher(cacher);
		}

		// Set transporter
		setTransporter(transporter);
	}
 
Example 10
Source File: MCRMetadataHistoryCommands.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
@MCRCommand(syntax = "build metadata history of base {0}",
    help = "build metadata history of all objects with base id {0}")
public static List<String> buildHistory(String baseId) {
    MCRMetadataStore store = MCRXMLMetadataManager.instance().getStore(baseId, true);
    if (store instanceof MCRVersioningMetadataStore) {
        LogManager.getLogger().info("Verify SVN history of {}", baseId);
        ((MCRVersioningMetadataStore) store).verify();
    }
    ExecutorService executorService = Executors.newWorkStealingPool();
    MCRSession currentSession = MCRSessionMgr.getCurrentSession();
    int maxId = store.getHighestStoredID();
    AtomicInteger completed = new AtomicInteger(maxId);
    IntStream.rangeClosed(1, maxId)
        .parallel()
        .mapToObj(i -> MCRObjectID.formatID(baseId, i))
        .map(MCRObjectID::getInstance)
        .map(id -> new MCRTransactionableCallable<>(Executors.callable(() -> {
            EntityManager em = MCREntityManagerProvider.getCurrentEntityManager();
            getHistoryItems(id).sequential().forEach(em::persist);
            completed.decrementAndGet();
        }), currentSession))
        .forEach(executorService::submit);
    executorService.shutdown();
    boolean waitToFinish = true;
    while (!executorService.isTerminated() && waitToFinish) {
        LogManager.getLogger().info("Waiting for history of {} objects/derivates.", completed.get());
        try {
            executorService.awaitTermination(10, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            waitToFinish = false;
        }
    }
    return Collections.emptyList();
}
 
Example 11
Source File: EtherNetIpShared.java    From ethernet-ip with Apache License 2.0 5 votes vote down vote up
/**
 * @return a shared {@link java.util.concurrent.ExecutorService}.
 */
public static synchronized ExecutorService sharedExecutorService() {
    if (SHARED_EXECUTOR == null) {
        SHARED_EXECUTOR = Executors.newWorkStealingPool();
    }
    return SHARED_EXECUTOR;
}
 
Example 12
Source File: AliasReCiterRetrievalEngine.java    From ReCiter with Apache License 2.0 5 votes vote down vote up
@Override
public boolean retrieveArticlesByDateRange(List<Identity> identities, Date startDate, Date endDate, RetrievalRefreshFlag refreshFlag) throws IOException {
	ExecutorService executorService = Executors.newWorkStealingPool(15);//Executors.newFixedThreadPool(10);
	for (Identity identity : identities) {
		executorService.execute(new AsyncRetrievalEngine(identity, startDate, endDate, refreshFlag));
	}
	executorService.shutdown();
	try {
		executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
	} catch (InterruptedException e) {
		slf4jLogger.error("Thread interrupted while waiting for retrieval to finish.");
		return false;
	}
	return true;
}
 
Example 13
Source File: CrfLogLikelihoodFunction.java    From CRF with MIT License 5 votes vote down vote up
private BigDecimal calculateSumOfLogNormalizations(CrfModel<K, G> model)
{
	ExecutorService executor = Executors.newWorkStealingPool();
	List<Future<BigDecimal>> futures = new LinkedList<>();
	BigDecimal sum = BigDecimal.ZERO;
	for (final List<? extends TaggedToken<K, G> > sentence : corpus)
	{
		futures.add(executor.submit(new Callable<BigDecimal>()
		{
			@Override
			public BigDecimal call() throws Exception
			{
				K[] sentenceAsArray = CrfUtilities.extractSentence(sentence);
				CrfRememberActiveFeatures<K, G> activeFeaturesForSentence = CrfRememberActiveFeatures.findForSentence(features, crfTags, sentenceAsArray);
				CrfForwardBackward<K, G> forwardBackward = new CrfForwardBackward<K, G>(model,sentenceAsArray,activeFeaturesForSentence);
				//forwardBackward.calculateForwardAndBackward();
				forwardBackward.calculateOnlyNormalizationFactor();
				
				return ArithmeticUtilities.log(forwardBackward.getCalculatedNormalizationFactor());
			}
		}));
	}
	for (Future<BigDecimal> future : futures)
	{
		try
		{
			sum = safeAdd(sum, future.get());
		}
		catch (InterruptedException | ExecutionException e)
		{
			throw new CrfException(e);
		}
	}
	
	return sum;
}
 
Example 14
Source File: T12_ThreadPool_6_WorkStealingPool.java    From ProjectStudy with MIT License 5 votes vote down vote up
public static void main(String[] args) throws IOException {
    ExecutorService service = Executors.newWorkStealingPool();
    System.out.println(Runtime.getRuntime().availableProcessors());
    service.execute(new R(1000));
    service.execute(new R(2000));
    service.execute(new R(2000));
    // daemon
    service.execute(new R(2000));
    service.execute(new R(2000));
    // 由于产生的是精灵线程(守护线程、后台线程),主线程不阻塞的话,看不到输出
    System.in.read();
}
 
Example 15
Source File: mdict.java    From mdict-java with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected ExecutorService OpenThreadPool(int thread_number) {
	return Executors.newWorkStealingPool();
}
 
Example 16
Source File: SSOZJ.java    From symja_android_library with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Main routine to setup, time, and display results for twin primes sieve.
 */
static void twinprimes_ssoz() {
	System.out.println(" Max threads = "+ (countProcessors()));
	long ts = epochTime();             // start timing sieve setup execution

	setSieveParameters(start_num, end_num);	// select PG and seg factor Bn for input range
	final int pairscnt = restwins.size();	// number of twin pairs for selected PG
	cnts = new long[pairscnt];				// array to hold count of tps for each thread
	lastwins = new long[pairscnt];			// array to hold largest tp for each thread

	if (PGparam.Lend < 49L) primes.add((5L));	// generate sieving primes
	else sozpg(Bsqrt(end_num));					// <= sqrt(end_num)

	PGparam.primesSize = primes.size();
	System.out.println("each "+ pairscnt+ " threads has nextp["+ 2+ " x "+ PGparam.primesSize + "] array");

	long twinscnt = 0;                 		// number of twin primes in range
	final long lo_range = restwins.getFirst() - 3; 	// lo_range = lo_tp - 1

	for (int tp : new int[]{3, 5, 11, 17}) {		// excluded low tp values for PGs used
		if (end_num.equals(THREE)) break;        	// if 3 end of range, no twin primes
		if (tp >= PGparam.Lstart && tp <= lo_range)  twinscnt++;
	}
	long te = epochTime() - ts;				// sieve setup time

	System.out.println("setup time = "+ te/1e3 + " secs");
	System.out.println("perform twinprimes ssoz sieve with s="+ S);

	ExecutorService stealingPool = Executors.newWorkStealingPool();

	List<Runnable> sieveTask = new ArrayList<>();
	// For printing progress
	final Callback<String> callback = System.out::print;

	AtomicInteger indx = new AtomicInteger();
	for (long r_hi : restwins) {    // for each twin pair row index
		sieveTask.add(() -> {
			callback.on("\r"+indx.get() + " of "+ pairscnt+ " threads done");
			twins_sieve(indx.getAndIncrement(), r_hi); // sieve selected twin pair restracks
		});
	}
	final long t1 = epochTime();				// start timing ssoz sieve execution
	// Implement parallel things
	try {
		stealingPool.submit(()->sieveTask.parallelStream().forEach(Runnable::run)).get();
	} catch (InterruptedException | ExecutionException e) {
		e.printStackTrace();
	} finally {
		// when all the threads finish
		stealingPool.shutdown();
		System.out.println("\r"+indx + " of "+ pairscnt+ " threads done");
	}
	// OR Simple parallel without specific pool
	// sieveTask.parallelStream().forEach(Runnable::run);

	long last_twin = 0L;            // find largest twin prime in range
	twinscnt += Arrays.stream(cnts).sum();
	last_twin = Arrays.stream(lastwins).max().orElse(0L);

	if (PGparam.Lend == 5L && twinscnt == 1) last_twin = 5L;
	long Kn = mod(PGparam.Lrange, KB);		// set number of resgroups in last slice
	if (Kn == 0) Kn = KB;              		// if multiple of seg size set to seg size

	cnts = null; lastwins = null;			// Free memory
	long t2 = epochTime() - t1;				// sieve execution time

	System.out.println("sieve time = "+ t2/1e3 + " secs");
	System.out.println("last segment = "+ Kn+ " resgroups; segment slices = "+ ((PGparam.Lrange-1) / KB + 1));
	System.out.println("total twins = "+ twinscnt+ "; last twin = "+ (last_twin-1) + "+/-1");

	System.out.println("total time = "+ (t2 + te)/1e3 + " secs\n");
}
 
Example 17
Source File: ProxyModule.java    From nomulus with Apache License 2.0 4 votes vote down vote up
@Provides
static ExecutorService provideExecutorService() {
  return Executors.newWorkStealingPool();
}
 
Example 18
Source File: DemoServletsAdapterTest.java    From keycloak with Apache License 2.0 4 votes vote down vote up
@Test
public void testTokenConcurrentRefresh() {
    RealmResource demoRealm = adminClient.realm("demo");
    RealmRepresentation demo = demoRealm.toRepresentation();

    demo.setAccessTokenLifespan(2);
    demo.setRevokeRefreshToken(true);
    demo.setRefreshTokenMaxReuse(0);

    demoRealm.update(demo);

    // Login
    tokenRefreshPage.navigateTo();
    assertTrue(testRealmLoginPage.form().isUsernamePresent());
    assertCurrentUrlStartsWithLoginUrlOf(testRealmPage);
    testRealmLoginPage.form().login("[email protected]", "password");
    assertCurrentUrlEquals(tokenRefreshPage);

    setAdapterAndServerTimeOffset(5, tokenRefreshPage.toString());

    BasicCookieStore cookieStore = new BasicCookieStore();
    BasicClientCookie jsessionid = new BasicClientCookie("JSESSIONID", driver.manage().getCookieNamed("JSESSIONID").getValue());

    jsessionid.setDomain("localhost");
    jsessionid.setPath("/");
    cookieStore.addCookie(jsessionid);

    ExecutorService executor = Executors.newWorkStealingPool();
    CompletableFuture future = CompletableFuture.completedFuture(null);

    try {
        for (int i = 0; i < 5; i++) {
            future = CompletableFuture.allOf(future, CompletableFuture.runAsync(() -> {
                try (CloseableHttpClient client = HttpClientBuilder.create().setDefaultCookieStore(cookieStore)
                        .build()) {
                    HttpUriRequest request = new HttpGet(tokenRefreshPage.getInjectedUrl().toString());
                    try (CloseableHttpResponse httpResponse = client.execute(request)) {
                        assertTrue("Token not refreshed", EntityUtils.toString(httpResponse.getEntity()).contains("accessToken"));
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }, executor));
        }
        
        future.join();
    } finally {
        executor.shutdownNow();
    }

    // Revert times
    setAdapterAndServerTimeOffset(0, tokenRefreshPage.toString());
}
 
Example 19
Source File: ParallelUtils.java    From JSAT with GNU General Public License v3.0 4 votes vote down vote up
public static void run(boolean parallel, int N, IndexRunnable ir)
{
    ExecutorService threadPool = Executors.newWorkStealingPool(SystemInfo.LogicalCores);
    run(parallel, N, ir, threadPool);
    threadPool.shutdownNow();
}
 
Example 20
Source File: VPTree.java    From JSAT with GNU General Public License v3.0 4 votes vote down vote up
@Override
   public void search(VectorCollection<V> Q, int numNeighbors, List<List<Integer>> neighbors, List<List<Double>> distances, boolean parallel)
   {
List<BoundedSortedList<IndexDistPair>> lists = new SimpleList<>();
List<Vec> queries = new SimpleList<>();
for(int i = 0; i < Q.size(); i++)
{
    queries.add(Q.get(i));
    lists.add(new BoundedSortedList<>(numNeighbors, numNeighbors));
}
List<Double> qi = dm.getAccelerationCache(queries, parallel);

DoubleList x = DoubleList.view(new double[Q.size()], Q.size());

ExecutorService threadPool = new FakeExecutor();
if(parallel)
    threadPool = Executors.newWorkStealingPool();
ModifiableCountDownLatch mcdl = new ModifiableCountDownLatch(1);

root.searchKNN(queries, numNeighbors, lists, x, qi, parallel, threadPool, mcdl);
mcdl.countDown();
try
{
    mcdl.await();
}
catch (InterruptedException ex)
{
    Logger.getLogger(VPTree.class.getName()).log(Level.SEVERE, null, ex);
}

neighbors.clear();
distances.clear();
for(BoundedSortedList<IndexDistPair> list : lists)
{
    IntList n = new IntList(numNeighbors);
    DoubleList d = new DoubleList(numNeighbors);
    
    for(int i = 0; i < list.size(); i++)
    {
	n.add(list.get(i).indx);
	d.add(list.get(i).dist);
    }
    
    neighbors.add(n);
    distances.add(d);
}
   }