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

The following are Jave code examples for showing how to use newWorkStealingPool() of the java.util.concurrent.Executors class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: r8   File: ThreadUtils.java   View Source Code Vote up 6 votes
public static ExecutorService getExecutorService(InternalOptions options) {
  if (options.numberOfThreads == options.NOT_SPECIFIED) {
    // This heuristic is based on measurements on a 32 core (hyper-threaded) machine.
    int threads = Integer.min(Runtime.getRuntime().availableProcessors(), 16) / 2;
    return Executors.newWorkStealingPool(threads);
  } else if (options.numberOfThreads == 1) {
    return Executors.newSingleThreadExecutor();
  } else {
    return Executors.newWorkStealingPool(options.numberOfThreads);
  }
}
 
Example 2
Project: outcomes   File: Executors3.java   View Source Code Vote up 6 votes
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 3
Project: greycat   File: ExecutorScheduler.java   View Source Code Vote up 5 votes
@Override
public void start() {
    if (_workers == -1) {
        this.service = Executors.newCachedThreadPool();
    } else {
        this.service = Executors.newWorkStealingPool(_workers);
    }
}
 
Example 4
Project: r8   File: ThreadUtils.java   View Source Code Vote up 5 votes
public static ExecutorService getExecutorService(int threads) {
  if (threads == 1) {
    return Executors.newSingleThreadExecutor();
  } else {
    return Executors.newWorkStealingPool(threads);
  }
}
 
Example 5
Project: r8   File: R8EntryPointTests.java   View Source Code Vote up 5 votes
@Test
public void testRun2Dir() throws IOException, CompilationException, ProguardRuleParserException {
  Path out = temp.newFolder("outdex").toPath();
  ExecutorService executor = Executors.newWorkStealingPool(2);
  try {
    R8.run(getCommand(out), executor);
  } finally {
    executor.shutdown();
  }
  Assert.assertTrue(Files.isRegularFile(out.resolve(FileUtils.DEFAULT_DEX_FILENAME)));
  Assert.assertTrue(Files.isRegularFile(testFlags.getParent().resolve(MAPPING)));
  Assert.assertTrue(Files.isRegularFile(testFlags.getParent().resolve(SEEDS)));
}
 
Example 6
Project: r8   File: R8EntryPointTests.java   View Source Code Vote up 5 votes
@Test
public void testRun2Zip() throws IOException, CompilationException, ProguardRuleParserException {
  Path out = temp.newFolder("outdex").toPath().resolve("dex.zip");
  ExecutorService executor = Executors.newWorkStealingPool(2);
  try {
    R8.run(getCommand(out), executor);
  } finally {
    executor.shutdown();
  }
  Assert.assertTrue(Files.isRegularFile(out));
  Assert.assertTrue(Files.isRegularFile(testFlags.getParent().resolve(MAPPING)));
  Assert.assertTrue(Files.isRegularFile(testFlags.getParent().resolve(SEEDS)));
}
 
Example 7
Project: rs-aggregator   File: Expedition.java   View Source Code Vote up 5 votes
/**
 * Gather ResourceSync Framework documents from a source in ResultIndexes.
 *
 * @param url the starting url to explore
 * @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) throws URISyntaxException, InterruptedException {
  URI uri = new URI(url);

  ExecutorService executor = Executors.newWorkStealingPool();

  List<Callable<ResultIndex>> callables = new ArrayList<>();
  callables.add(() -> exploreWellKnown(uri));
  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 8
Project: mmm   File: AffinityMetric.java   View Source Code Vote up 5 votes
public AffinityMetric(AffinityMetricConfiguration<LabelType> affinityMetricConfiguration) {
    maximalAffinity = affinityMetricConfiguration.getMaximalAffinity();
    levelOfParallelism = affinityMetricConfiguration.getLevelOfParallelism();
    executorService = (levelOfParallelism == -1) ? Executors.newWorkStealingPool() : Executors.newWorkStealingPool(levelOfParallelism);
    representationSchemeType = affinityMetricConfiguration.getRepresentationSchemeType();
    atomFilter = affinityMetricConfiguration.getAtomFilterType().getFilter();
    alignWithinClusters = affinityMetricConfiguration.isAlignWithinClusters();

    affinityItemsets = new HashMap<>();
}
 
Example 9
Project: mmm   File: AdherenceMetric.java   View Source Code Vote up 5 votes
public AdherenceMetric(List<DataPoint<LabelType>> dataPoints, AdherenceMetricConfiguration<LabelType> adherenceMetricConfiguration) {
    super(dataPoints, adherenceMetricConfiguration.getRepresentationSchemeType());
    distributions = Collections.synchronizedMap(new HashMap<>());
    desiredSquaredExtent = adherenceMetricConfiguration.getDesiredExtent() * adherenceMetricConfiguration.getDesiredExtent();
    squaredExtentDelta = adherenceMetricConfiguration.getDesiredExtentDelta() * adherenceMetricConfiguration.getDesiredExtentDelta();
    maximalAdherence = adherenceMetricConfiguration.getMaximalAdherence();
    vertexOne = adherenceMetricConfiguration.isVertexOne();
    levelOfParallelism = adherenceMetricConfiguration.getLevelOfParallelism();
    executorService = (levelOfParallelism == -1) ? Executors.newWorkStealingPool() : Executors.newWorkStealingPool(levelOfParallelism);
}
 
Example 10
Project: mmm   File: ConsensusMetric.java   View Source Code Vote up 5 votes
public ConsensusMetric(ConsensusMetricConfiguration<LabelType> consensusMetricConfiguration) {
    maximalConsensus = consensusMetricConfiguration.getMaximalConsensus();
    clusterCutoff = consensusMetricConfiguration.getClusterCutoffValue();
    levelOfParallelism = consensusMetricConfiguration.getLevelOfParallelism();
    executorService = (levelOfParallelism == -1) ? Executors.newWorkStealingPool() : Executors.newWorkStealingPool(levelOfParallelism);
    atomFilter = consensusMetricConfiguration.getAtomFilterType().getFilter();
    representationSchemeType = consensusMetricConfiguration.getRepresentationSchemeType();
    alignWithinClusters = consensusMetricConfiguration.isAlignWithinClusters();

    distributions = new HashMap<>();
    clusteredItemsets = new HashMap<>();
}
 
Example 11
Project: mmm   File: CohesionMetric.java   View Source Code Vote up 5 votes
public CohesionMetric(List<DataPoint<LabelType>> dataPoints, CohesionMetricConfiguration<LabelType> cohesionMetricConfiguration) {
    super(dataPoints, cohesionMetricConfiguration.getRepresentationSchemeType());
    distributions = new HashMap<>();
    maximalCohesion = cohesionMetricConfiguration.getMaximalCohesion();
    vertexOne = cohesionMetricConfiguration.isVertexOne();
    levelOfParallelism = cohesionMetricConfiguration.getLevelOfParallelism();
    executorService = (levelOfParallelism == -1) ? Executors.newWorkStealingPool() : Executors.newWorkStealingPool(levelOfParallelism);
}
 
Example 12
Project: northpine   File: ScrapeJob.java   View Source Code Vote up 5 votes
/**
 * @param layerUrl Does not include "/query" appended to end of url to layer.
 */
public ScrapeJob(String layerUrl) {
  executor = Executors.newWorkStealingPool();
  current = new AtomicInteger();
  total = new AtomicInteger();
  failed = new AtomicBoolean( false);
  this.layerUrl = layerUrl ;
  this.queryUrlStr = layerUrl + "/query";
  this.layerName = getLayerName();
  this.outputFileBase =  OUTPUT_FOLDER + "/" + layerName;
  this.outputZip =  OUTPUT_FOLDER + "/" + layerName + ".zip";
  this.deleteQueue = new ConcurrentLinkedDeque<>();
}
 
Example 13
Project: StellarCrush   File: GameState.java   View Source Code Vote up 5 votes
/**
 * Utility method which check if some objects collides and merge then in that case.
 * @return The number of object that was merged during this check.
 */
private int checkIntegrity()
{
    ExecutorService taskExecutor = Executors.newWorkStealingPool();
    Map<GameObject, Collection<GameObject>> collisions = calculateCollisions(taskExecutor);
    taskExecutor.shutdown();
    try{
        taskExecutor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
    } catch (InterruptedException ignored) {
        ignored.printStackTrace();
    }
    int i = 0;
    for(Map.Entry<GameObject, Collection<GameObject>> entry : collisions.entrySet())
    {
        GameObject a = entry.getKey();
        Collection<GameObject> collides = entry.getValue();
        for(GameObject b : collides)
        {
            i++;
            collisions.get(b).remove(a);

            a.applyCollide(b);
            GameObjectLibrary.mergeAsteroid(a, b);
            removeGameObject(b);
        }
    }
    return i;
}
 
Example 14
Project: outcomes   File: Executors3.java   View Source Code Vote up 5 votes
private 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();
}
 
Example 15
Project: r8   File: Bisect.java   View Source Code Vote up 4 votes
public DexProgramClass run() throws Exception {
  // Setup output directory (or write to a temp dir).
  Path output;
  if (options.output != null) {
    output = options.output.toPath();
  } else {
    File temp = File.createTempFile("bisect", "", new File("/tmp"));
    temp.delete();
    temp.mkdir();
    output = temp.toPath();
  }

  ExecutorService executor = Executors.newWorkStealingPool();
  try {
    DexApplication goodApp = readApp(options.goodBuild, executor);
    DexApplication badApp = readApp(options.badBuild, executor);

    File stateFile = options.stateFile != null
        ? options.stateFile
        : output.resolve("bisect.state").toFile();

    // Setup initial (or saved) bisection state.
    BisectState state = new BisectState(goodApp, badApp, stateFile);
    if (options.stateFile != null) {
      state.read();
    }

    // If a "previous" result is supplied on the command line, record it.
    if (options.result != Result.UNKNOWN) {
      state.setPreviousResult(options.result);
    }

    // Setup post-build command.
    Command command = null;
    if (options.command != null) {
      command = (application) -> {
        writeApp(application, output, executor);
        return runCommand(output);
      };
    }

    // Run bisection.
    return run(state, command, output, executor);
  } finally {
    executor.shutdown();
  }
}
 
Example 16
Project: jLZJD   File: Main.java   View Source Code Vote up 4 votes
public void run(String... args) throws IOException, InterruptedException
{
    JCommander jc = new JCommander(this);
    jc.parse(args);
    ex = Executors.newWorkStealingPool(Math.max(1, threads));
    if(!alt_output.getPath().equals(""))
        System.setOut(new PrintStream(new BufferedOutputStream(new FileOutputStream(alt_output)), true));
    
    //collect all the files we will be hashing
    List<File> toHash = new ArrayList<>();
    for(File candidate : parameters)
        if(candidate.isFile())
            toHash.add(candidate);
        else if(candidate.isDirectory() && goDeep)
            Files.walk(candidate.toPath()).filter(Files::isRegularFile).forEach(c -> toHash.add(c.toFile()));
    
    if(toCompare)
    {
        if(parameters.size() > 2 || parameters.isEmpty())
            throw new IllegalArgumentException("Can only compare at most two indexes at a time!");
        
        List<int[]> hashesA = new ArrayList<>();
        List<String> filesA = new ArrayList<>();
        
        List<int[]> hashesB = new ArrayList<>();
        List<String> filesB = new ArrayList<>();
        
        readHashesFromFile(parameters.get(0), hashesA, filesA);
        if(parameters.size() == 2)
            readHashesFromFile(parameters.get(1), hashesB, filesB);
        else
        {
            hashesB = hashesA;
            filesB = filesA;
        }
        
        compare(hashesA, filesA, hashesB, filesB);
    }
    else if(genCompare)
        genComp(toHash);
    else
        hashFiles(toHash);
    
    ex.shutdownNow();
}
 
Example 17
Project: rockscript   File: MonitoringExecutor.java   View Source Code Vote up 4 votes
public static MonitoringExecutor createDefault(EngineLogStore engineLogStore) {
  return new MonitoringExecutor(engineLogStore, Executors.newWorkStealingPool());
}
 
Example 18
Project: mmm   File: DistributionSampler.java   View Source Code Vote up 4 votes
DistributionSampler(ItemsetMiner<LabelType> itemsetMiner, Class<? extends DistributionMetric> distributionMetricType, int levelOfParallelism, int sampleSize) {

        super(itemsetMiner.getEvaluationMetrics().stream()
                          .filter(ExtractionMetric.class::isInstance)
                          .map(ExtractionMetric.class::cast)
                          .map(ExtractionMetric::getRepresentationSchemeType)
                          .filter(Objects::nonNull)
                          .findAny()
                          .orElse(null));

        this.distributionMetricType = distributionMetricType;
        this.levelOfParallelism = levelOfParallelism;
        this.sampleSize = sampleSize;

        // work with copy of data points such that original labels stay the same
        dataPoints = itemsetMiner.getDataPoints().stream()
                                 .map(DataPoint::getCopy)
                                 .collect(Collectors.toList());
        itemsets = itemsetMiner.getTotalItemsets();
        backgroundDistributions = new HashMap<>();
        executorService = (levelOfParallelism == -1) ? Executors.newWorkStealingPool() : Executors.newWorkStealingPool(levelOfParallelism);

        logger.info("distribution sampler initialized for distribution metric type " + distributionMetricType.getSimpleName());

        // get associated extraction metric and store parameters
        ExtractionMetric extractionMetric = itemsetMiner.getEvaluationMetrics().stream()
                                                        .filter(ExtractionMetric.class::isInstance)
                                                        .map(ExtractionMetric.class::cast)
                                                        .findAny()
                                                        .orElseThrow(() -> new DistributionSamplerException("failed to determine used extraction metric"));
        extractionMetricType = extractionMetric.getClass();
        vertexOne = extractionMetric.isVertexOne();

        if (distributionMetricType == ConsensusMetric.class) {
            clusterCutoff = itemsetMiner.getItemsetMinerConfiguration().getExtractionDependentMetricConfigurations().stream()
                                        .filter(ConsensusMetricConfiguration.class::isInstance)
                                        .map(ConsensusMetricConfiguration.class::cast)
                                        .findAny()
                                        .orElseThrow(() -> new DistributionSamplerException("failed to determine extraction metric type"))
                                        .getClusterCutoffValue();
        }

        runBackgroundSampling();
    }