Multi-threaded java loop with collection results

advertisements

sorry for limited code, as i have quite no idea how to do it, and parts of the code are not a code, just an explanation what i need. The base is:

arrayList<double> resultTopTen = new arrayList<double();
arrayList<double> conditions = new arrayList<double(); // this arrayList can be of a very large size milion+, gets filled by different code

double result = 0;

for (int i = 0, i < conditions.size(), i++){   //multithread this

loopResult = conditions.get(i) + 5; 

   if (result.size() < 10){
      resultTopTen.add(loopResult);
   }
   else{
      //this part i don't know, if this loopResult belongs to the TOP 10 loopResults so far, just by size, replace the smallest one with current, so that i will get updated resultTopTen in this point of loop. 

   }
}

  • loopResult = conditions.get(i) + 5; part is just an example, calculation is different, in fact it is not even double, so it is not possible simply to sort conditions and go from there.
  • for (int i = 0, i < conditions.size(), i++) part means i have to iterate through input condition list, and execute the calculation and get result for every condition in conditionlist, Don't have to be in order at all.
  • The multithreading part is the thing i have really no idea how to do, but as the conditions arrayList is really large, i would like to calculate it somehow in parallel, as if i do it just as it is in the code in a simple loop in 1 thread, i wont get my computing resources utilized fully. The trick here is how to split the conditions, and then collect result. For simplicity if i would like to do it in 2 threads, i would split conditions in half, make 1 thread do the same loop for 1st half and second for second, i would get 2 resultTopTen, which i can put together afterwards, But much better would be to split the thing in to as many threads as system resources provide(for example until cpu ut <90%, ram <90%). Is that possible?

Use parallel stream of Java 8.

static class TopN<T> {

    final TreeSet<T> max;
    final int size;

    TopN(int size, Comparator<T> comparator) {
        this.max = new TreeSet<>(comparator);
        this.size = size;
    }

    void add(T n) {
        max.add(n);
        if (max.size() > size)
            max.remove(max.last());
    }

    void combine(TopN<T> o) {
        for (T e : o.max)
            add(e);
    }

}

public static void main(String[] args) {
    List<Double> conditions = new ArrayList<>();
    // add elements to conditions
    TopN<Double> maxN = conditions.parallelStream()
        .map(d -> d + 5)    // some calculation
        .collect(() -> new TopN<Double>(10, (a, b) -> Double.compare(a, b)),
            TopN::add, TopN::combine);
    System.out.println(maxN.max);
}

Class TopN holds top n items of T.

This code prints minimum top 10 in conditions (add 5 to each element).