/Parallelized.java Secret
Created
July 27, 2021 16:53
This file contains 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
package Test; | |
import java.util.concurrent.ExecutorService; | |
import java.util.concurrent.Executors; | |
import java.util.concurrent.TimeUnit; | |
import org.junit.runners.Parameterized; | |
import org.junit.runners.model.RunnerScheduler; | |
public class Parallelized extends Parameterized { | |
private static class ThreadPoolScheduler implements RunnerScheduler { | |
private ExecutorService executor; | |
public ThreadPoolScheduler() { | |
String threads = System.getProperty("junit.parallel.threads", "15"); | |
int numThreads = Integer.parseInt(threads); | |
executor = Executors.newFixedThreadPool(numThreads); | |
} | |
public void finished() { | |
executor.shutdown(); | |
try { | |
executor.awaitTermination(10, TimeUnit.MINUTES); | |
} catch (InterruptedException exc) { | |
throw new RuntimeException(exc); | |
} | |
} | |
public void schedule(Runnable childStatement) { | |
executor.submit(childStatement); | |
} | |
} | |
public Parallelized(Class<?> klass) throws Throwable { | |
super(klass); | |
setScheduler(new ThreadPoolScheduler()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment