Advertisement
Guest User

Untitled

a guest
Jul 1st, 2011
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.60 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.concurrent.*;
  3.  
  4. public class Main {
  5.  
  6.     private static final int MAX_THREADS = 4;
  7.  
  8.     private static byte[] data = new byte[500 * 1024 * 1024];
  9.     private static ExecutorService executor = Executors.newCachedThreadPool();
  10.  
  11.     public static void main(String[] args) throws InterruptedException, ExecutionException {
  12.         for (int k = 0; k < 50; k++) {
  13.             int numberOfThreads = k % MAX_THREADS + 1;
  14.  
  15.             long start = System.currentTimeMillis();
  16.             runBenchmark(numberOfThreads);
  17.             long end = System.currentTimeMillis();
  18.  
  19.             checkDataIsConsistent();
  20.             printResults(numberOfThreads, start, end);
  21.         }
  22.         executor.shutdown();
  23.     }
  24.  
  25.     private static void runBenchmark(int numberOfThreads) throws InterruptedException, ExecutionException {
  26.         List<Future<?>> futures = new ArrayList<Future<?>>();
  27.         for (int i = 0; i < numberOfThreads; i++) {
  28.             futures.add(executor.submit(new Worker(i, numberOfThreads)));
  29.         }
  30.         for (Future<?> future : futures) {
  31.             future.get();
  32.         }
  33.     }
  34.  
  35.     private static void checkDataIsConsistent() {
  36.         int reference = data[0];
  37.         for (int i = 0; i < data.length; i++) {
  38.             int current = data[i];
  39.             if (current != reference) {
  40.                 throw new AssertionError("inconsistent data at index " + i + ": " + current + " != " + reference);
  41.             }
  42.         }
  43.     }
  44.  
  45.     private static void printResults(int numberOfThreads, long start, long end) {
  46.         if (numberOfThreads == 1) {
  47.             System.out.println();
  48.         }
  49.         System.out.println(numberOfThreads + " Thread(s): " + (end - start) + " ms");
  50.     }
  51.  
  52.  
  53.     private static class Worker implements Runnable {
  54.  
  55.         private final int workerId;
  56.         private final int totalWorkers;
  57.  
  58.         public Worker(int workerId, int totalWorkers) {
  59.             this.workerId = workerId;
  60.             this.totalWorkers = totalWorkers;
  61.         }
  62.  
  63.         @Override
  64.         public void run() {
  65.             int chunkSize = data.length / totalWorkers;
  66.             int start = chunkSize * workerId;
  67.             int end;
  68.             if (workerId == totalWorkers - 1) {
  69.                 end = data.length;
  70.             } else {
  71.                 end = start + chunkSize;
  72.             }
  73.  
  74.             for (int i = start; i < end; i++) {
  75.                 doSomeComputation(i);
  76.             }
  77.         }
  78.  
  79.         private void doSomeComputation(int i) {
  80.             data[i] = (byte) Math.sqrt(data[i] + 42);
  81.         }
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement