Last active
May 26, 2023 11:47
-
-
Save joelibaceta/efa8bf6c0dcb3451ca478d8b92098e78 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
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