public TreeSet computeCorrelation(StockTicker correlationForTicker, int period, CorrelationType correlationType) { log.debug("Finding most correlated stocks for: " + correlationForTicker); if (period != 10 && period != 30 && period != 60 && period != 90) { throw new IllegalArgumentException("Wrong period"); } Objects.requireNonNull(correlationForTicker); Objects.requireNonNull(correlationType); step = 0; isComputing = true; Correlator correlator = getCorrelatorImpl(correlationType); TreeSet correlationTreeSet = new TreeSet(); final EnumSet tickersToScan = complementOf(EnumSet.of(correlationForTicker)); final double[] sourceClosePrices = getClosePrices(getContent(correlationForTicker, period)); ExecutorService executor = Executors.newFixedThreadPool(2); for (StockTicker ticker : tickersToScan) { executor.submit(() -> { double[] targetClosePrices = getClosePrices(getContent(ticker, period)); double correlation = correlator.correlate(sourceClosePrices, targetClosePrices); StockStatistic statistic = new StockStatistic(correlation, ticker); correlationTreeSet.add(statistic); increaseStep(); }); } executor.shutdown(); try { executor.awaitTermination(1, TimeUnit.MINUTES); } catch (InterruptedException e) { e.printStackTrace(); } this.step = 0; this.isComputing = false; return correlationTreeSet; }