Skip to content

Instantly share code, notes, and snippets.

@alonsoir
Created February 12, 2019 14:37
Show Gist options
  • Save alonsoir/b8eebbddfa88cc9cd66804b17434a96e to your computer and use it in GitHub Desktop.
Save alonsoir/b8eebbddfa88cc9cd66804b17434a96e to your computer and use it in GitHub Desktop.
// cores can be four in my case because i have an i7 hyperthread with four physical cores.
// numThreadsToRun can be as much as you can. I usually run this code with this value set up to 10000000
// bigger the number, more time you need to finish the main task.
ExecutorService executor = Executors.newFixedThreadPool(cores * 2);
for (int i = 1; i <= numThreadsToRun; i++) {
if (isEmExp) {
Future<?> future = executor.submit(Utils.showSorteredValuesReversedOrder(pathToEM));
future.get(5, TimeUnit.SECONDS);
}
else if (isEm) {
Future<?> future = executor.submit(Utils.calculateRandomEM(pathToEM));
future.get(5, TimeUnit.SECONDS);
} else if (isPrimitiva) {
Future<?> future = executor.submit(Utils.calculatePrimitiva(pathToPrimitiva));
future.get(5, TimeUnit.SECONDS);
}
else if (all) {
Future<?> futureRandomEM = executor.submit(Utils.calculateRandomEM(pathToEM));
Future<?> futurePrimitive = executor.submit(Utils.calculatePrimitiva(pathToPrimitiva));
futureRandomEM.get(5, TimeUnit.SECONDS);
futurePrimitive.get(5, TimeUnit.SECONDS);
} else if (allWithExperimental) {
Future<?> futureSortered = executor.submit(Utils.showSorteredValuesReversedOrder(pathToEM));
Future<?> futurePrimitive = executor.submit(Utils.calculatePrimitiva(pathToPrimitiva));
futureSortered.get(5, TimeUnit.SECONDS);
futurePrimitive.get(5, TimeUnit.SECONDS);
}
}
shutdownThreads(executor);
private static void shutdownThreads(ExecutorService executor) {
try {
System.out.println("attempt to shutdown executor");
executor.shutdown();
executor.awaitTermination(5, TimeUnit.MINUTES);
} catch (InterruptedException e) {
System.err.println("tasks interrupted");
System.exit(-1);
} finally {
if (!executor.isTerminated()) {
System.err.println("cancel non-finished tasks");
}
executor.shutdownNow();
System.out.println("shutdown finished");
}
}
...
// Utils class, i only show one method...
static Runnable calculatePrimitiva(String pathToPrimitiva) {
Runnable runnable = () -> {
try {
List<EMPojo> myListEMPojo = Utils.processInputFile(pathToPrimitiva);
final Comparator<EMPojo> compTotal2017 = (p1, p2) -> Integer.compare(p1.getTotal_2017(),
p2.getTotal_2017());
final Comparator<EMPojo> compTotal2018 = (p1, p2) -> Integer.compare(p1.getTotal_2018(),
p2.getTotal_2018());
long maxSize = 7l;
System.out.println("Primitiva 2017. ");
myListEMPojo.stream().sorted(compTotal2017.reversed()) // sort from max to min
.limit(maxSize).forEach(e -> System.out.println(e.toString()));
System.out.println("Primitiva 2018. ");
myListEMPojo.stream().sorted(compTotal2018.reversed()) // sort from max to min
.limit(maxSize).forEach(e -> System.out.println(e.toString()));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
};
return runnable;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment