Advertisement
dig090

Comparison of atomic boolean arrays

Mar 6th, 2014
460
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.33 KB | None | 0 0
  1. package com.j256;
  2.  
  3. import java.util.concurrent.ExecutorService;
  4. import java.util.concurrent.Executors;
  5. import java.util.concurrent.TimeUnit;
  6. import java.util.concurrent.atomic.AtomicBoolean;
  7. import java.util.concurrent.atomic.AtomicIntegerArray;
  8.  
  9. import org.junit.Test;
  10.  
  11. public class BooleanTester {
  12.  
  13.     private final static int NUM_ATOMICS = 10;
  14.     private long NUM_CYCLES = 10000000;
  15.  
  16.     private final AtomicIntegerArray array1 = new AtomicIntegerArray(NUM_ATOMICS);
  17.     private final AtomicBoolean[] array2 = new AtomicBoolean[NUM_ATOMICS];
  18.  
  19.     {
  20.         for (int i = 0; i < array2.length; i++) {
  21.             array2[i] = new AtomicBoolean();
  22.         }
  23.     }
  24.  
  25.     @Test
  26.     public void test() throws Exception {
  27.  
  28.         // warm up hotspot
  29.         NUM_CYCLES = 1000000;
  30.         runTest();
  31.  
  32.         // really do the test
  33.         NUM_CYCLES = 10000000;
  34.         runTest();
  35.     }
  36.  
  37.     private void runTest() throws Exception {
  38.         long start = System.currentTimeMillis();
  39.         ExecutorService threadPool = Executors.newCachedThreadPool();
  40.         for (int i = 0; i < 100; i++) {
  41.             threadPool.submit(new AtomicIntegerArrayRunnable());
  42.         }
  43.         threadPool.shutdown();
  44.         threadPool.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
  45.         System.out.println("AtomicIntegerArrayRunnable in " + (System.currentTimeMillis() - start) + " millis");
  46.  
  47.         /* ======================== */
  48.  
  49.         start = System.currentTimeMillis();
  50.         threadPool = Executors.newCachedThreadPool();
  51.         for (int i = 0; i < 100; i++) {
  52.             threadPool.submit(new AtomicBooleanArrayRunnable());
  53.         }
  54.         threadPool.shutdown();
  55.         threadPool.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
  56.         System.out.println("AtomicBooleanArrayRunnable in " + (System.currentTimeMillis() - start) + " millis");
  57.     }
  58.  
  59.     private class AtomicIntegerArrayRunnable implements Runnable {
  60.         @Override
  61.         public void run() {
  62.             for (long i = 0; i < NUM_CYCLES; i++) {
  63.                 if (i % 2 == 0) {
  64.                     array1.compareAndSet((int) (i % NUM_ATOMICS), 0, 1);
  65.                 } else {
  66.                     array1.compareAndSet((int) (i % NUM_ATOMICS), 1, 0);
  67.                 }
  68.             }
  69.         }
  70.     }
  71.  
  72.     private class AtomicBooleanArrayRunnable implements Runnable {
  73.         @Override
  74.         public void run() {
  75.             for (long i = 0; i < NUM_CYCLES; i++) {
  76.                 if (i % 2 == 0) {
  77.                     array2[(int) (i % NUM_ATOMICS)].compareAndSet(false, true);
  78.                 } else {
  79.                     array2[(int) (i % NUM_ATOMICS)].compareAndSet(true, false);
  80.                 }
  81.             }
  82.         }
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement