Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.lang.management.ManagementFactory;
- import java.lang.management.ThreadMXBean;
- import java.util.List;
- import java.util.ArrayList;
- import java.util.concurrent.*;
- import java.util.concurrent.atomic.AtomicLong;
- public class ThreadingStressTest {
- private static ThreadMXBean tmx = ManagementFactory.getThreadMXBean();
- public static void main(String args[]) throws InterruptedException, ExecutionException {
- new ThreadingStressTest();
- }
- public ThreadingStressTest() throws InterruptedException, ExecutionException{
- int upperBound = 64*1024;
- int chunkSize = 64;
- int chunkCount = upperBound / chunkSize;
- int upperThreadCount = 24;
- System.out.printf("Parallel search for all primes [3 - %d] on %d-core CPU "
- + "using %d chunks sized %d\n",
- upperBound,
- Runtime.getRuntime().availableProcessors(),
- chunkCount,
- chunkSize);
- for(int i = 0; i < 500; i++){
- findPrimes(upperBound, chunkSize, 1);
- findPrimes(upperBound, chunkSize, 2);
- for(int threadCount = 4; threadCount <= upperThreadCount; threadCount+=4){
- findPrimes(upperBound, chunkSize, threadCount);
- }
- }
- }
- private void findPrimes(int upperBound, int chunkSize, int threadCount) throws InterruptedException,
- ExecutionException {
- final long before = System.nanoTime();
- final List<Integer> primes = new ArrayList<Integer>(upperBound);
- final AtomicLong cpuTime = new AtomicLong();
- final ExecutorService pool = Executors.newFixedThreadPool(threadCount);
- final List<Future<PrimeResult>> futureResults = new ArrayList<Future<PrimeResult>>(threadCount);
- int chunkCount = upperBound / chunkSize;
- for (int i = 0; i < chunkCount; i++) {
- futureResults.add( pool.submit(new ComputeChunk(
- upperBound / chunkCount * i,
- upperBound / chunkCount))
- );
- }
- for (Future<PrimeResult> futureResult : futureResults) {
- primes.addAll(futureResult.get().getPrimes());
- cpuTime.addAndGet(futureResult.get().getCpuTime());
- }
- pool.shutdown();
- long after = System.nanoTime();
- double secondsElapsed = (after - before) / 1000000000.0d;
- double normalized = cpuTime.get()/ 1000000000.0d;
- System.out.printf("%d primes found using %d thread(s) in %.1fsec realtime, %.1f cpu-time. Scaling %.2f.\n",
- primes.size(),
- threadCount,
- secondsElapsed,
- normalized,
- normalized/secondsElapsed
- );
- }
- class ComputeChunk implements Callable<PrimeResult> {
- private int start, end;
- private List<Integer> primes = new ArrayList<Integer>();
- public ComputeChunk(int start, int length) {
- this.start = start;
- this.end = length + start;
- }
- @Override
- public PrimeResult call() {
- long cpuTime = tmx.getCurrentThreadCpuTime();
- for (int i = start; i < end; i++) {
- if (isPrime(i)) {
- primes.add(i);
- }
- }
- return new PrimeResult(primes, tmx.getCurrentThreadCpuTime() - cpuTime);
- }
- }
- private static boolean isPrime(int candidate) {
- // Eliminate even no.
- if ((candidate & 1) == 0) {
- return false;
- }
- // Find the upper bound we need to test for
- long ubound = (long) Math.sqrt(candidate) + 2;
- for (int i = 3; i < ubound; i += 2) {
- if ((candidate % i) == 0) {
- return false;
- }
- }
- return true;
- }
- private class PrimeResult{
- private List<Integer> primes;
- private long cpuTime;
- public PrimeResult(List<Integer> primes, long cpuTime){
- this.primes = primes;
- this.cpuTime = cpuTime;
- }
- public List<Integer> getPrimes(){
- return primes;
- }
- public long getCpuTime(){
- return cpuTime;
- }
- }
- }
Add Comment
Please, Sign In to add comment