Skip to content

Instantly share code, notes, and snippets.

@joelibaceta
Last active May 26, 2023 11:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joelibaceta/efa8bf6c0dcb3451ca478d8b92098e78 to your computer and use it in GitHub Desktop.
Save joelibaceta/efa8bf6c0dcb3451ca478d8b92098e78 to your computer and use it in GitHub Desktop.
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Experiment {
private static final long iterations = 1000000000;
private static final long updateInterval = 1000000000;
public static void main(String[] args) {
int numThreads = Runtime.getRuntime().availableProcessors(); // Obtener el número de núcleos disponibles
ExecutorService executor = Executors.newFixedThreadPool(numThreads);
long startTime = System.currentTimeMillis();
for (int i = 0; i < numThreads; i++) {
final long start = i * (iterations / numThreads);
final long end = (i + 1) * (iterations / numThreads);
executor.execute(() -> processIterations(start, end));
}
executor.shutdown();
while (!executor.isTerminated()) {
// Esperar a que todos los hilos terminen
}
long endTime = System.currentTimeMillis();
long totalTime = endTime - startTime;
System.out.println("Tiempo total: " + totalTime + " milisegundos");
}
private static void processIterations(long start, long end) {
for (long i = start; i < end; i++) {
if (i % updateInterval == 0 && i > 0) {
System.out.println(i);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment