Index: lams_central/src/java/org/lamsfoundation/lams/webservice/SPEnrolmentServlet.java =================================================================== diff -u -re6faf35d647feed7b62d2dc0ee17bf1bb5af917b -rcfa39638c61dc4a0e9f93dc19cb40fb4c6db089a --- lams_central/src/java/org/lamsfoundation/lams/webservice/SPEnrolmentServlet.java (.../SPEnrolmentServlet.java) (revision e6faf35d647feed7b62d2dc0ee17bf1bb5af917b) +++ lams_central/src/java/org/lamsfoundation/lams/webservice/SPEnrolmentServlet.java (.../SPEnrolmentServlet.java) (revision cfa39638c61dc4a0e9f93dc19cb40fb4c6db089a) @@ -33,7 +33,6 @@ import java.util.Comparator; import java.util.HashSet; import java.util.Iterator; -import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -347,6 +346,7 @@ } }); } while (elementsRemaining); + logger.info("Processed " + mappingsProcessed.get() + " entries and finished"); } finally { HibernateSessionManager.closeSession(); } @@ -409,6 +409,7 @@ // never return 0 as course will considered a duplicate return courseSizeDifference == 0 && !c1.equals(c2) ? 1 : courseSizeDifference; }; + Collection>> spliterators = splitCollection( allParsedCourseMapping.entrySet(), parsedCourseSizeComparator); List> futures = new ArrayList<>(spliterators.size()); @@ -445,6 +446,7 @@ } }); } while (elementsRemaining); + logger.info("Processed " + mappingsProcessed.get() + " entries and finished"); } finally { HibernateSessionManager.closeSession(); } @@ -929,37 +931,35 @@ } /** - * Splits collection into as many spliterators as there are threads + * Splits collection into as many spliterators as there are threads, with balanced threads' load */ - private Collection> splitCollection(Collection collection, Comparator comparator) { - // if there is only 10 entries, do it in one thread - int threadCount = 1; + private Collection> splitCollection(Collection sourceCollection, Comparator comparator) { + if (sourceCollection.size() < 10) { + return Set.of(sourceCollection.spliterator()); + } - if (collection.size() > 10) { - threadCount = this.threadCount; - - if (comparator != null) { - Set treeSet = new TreeSet<>(comparator); - treeSet.addAll(collection); - collection = treeSet; - } - - List shuffledCollection = new ArrayList<>(collection); + if (comparator == null) { + List shuffledCollection = new ArrayList<>(sourceCollection); Collections.shuffle(shuffledCollection); - collection = shuffledCollection; + sourceCollection = shuffledCollection; + } else { + Set treeSet = new TreeSet<>(comparator); + treeSet.addAll(sourceCollection); + sourceCollection = treeSet; } - LinkedList> spliterators = new LinkedList<>(); - spliterators.add(collection.spliterator()); - for (int threadIndex = 1; threadIndex < threadCount; threadIndex++) { - Spliterator spliterator = spliterators.removeFirst(); - spliterators.add(spliterator); - Spliterator anotherSpliterator = spliterator.trySplit(); - if (anotherSpliterator != null) { - spliterators.add(anotherSpliterator); - } + List> collectionList = new ArrayList<>(threadCount); + for (int threadIndex = 0; threadIndex < threadCount; threadIndex++) { + collectionList.add(new ArrayList<>(sourceCollection.size() / threadCount + 1)); } - return spliterators; + + int listIndex = 0; + for (T element : sourceCollection) { + collectionList.get(listIndex % threadCount).add(element); + listIndex++; + } + + return collectionList.stream().collect(Collectors.mapping(List::spliterator, Collectors.toList())); } @Override