Skip to content

Instantly share code, notes, and snippets.

@franz1981
Created December 12, 2018 16:05
Show Gist options
  • Save franz1981/544c8f5bc892a02cf659c7f2bd699436 to your computer and use it in GitHub Desktop.
Save franz1981/544c8f5bc892a02cf659c7f2bd699436 to your computer and use it in GitHub Desktop.
public static void main(String[] args) {
final int parallelism = 8;
final ExecutorService executor = Executors.newFixedThreadPool(parallelism);
final CyclicBarrier started = new CyclicBarrier(parallelism);
final Callable<Long> task = () -> {
started.await();
final Thread current = Thread.currentThread();
long executions = 0;
while (!current.isInterrupted()) {
//quello che deve fare sul DM
executions++;
}
return executions;
};
final ArrayList<Future<Long>> tasks = new ArrayList<>(parallelism);
for (int i = 0; i < parallelism; i++) {
tasks.add(executor.submit(task));
}
executor.shutdown();
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
tasks.forEach(future -> future.cancel(true));
}));
}
@lucamolteni
Copy link

OMG this code is amazing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment