Created
December 12, 2018 16:05
-
-
Save franz1981/544c8f5bc892a02cf659c7f2bd699436 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
})); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
OMG this code is amazing