Advertisement
dig090

Increment Locking Comparison

Sep 11th, 2012
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.91 KB | None | 0 0
  1. package com.j256;
  2.  
  3. import java.util.Random;
  4. import java.util.concurrent.ExecutorService;
  5. import java.util.concurrent.Executors;
  6. import java.util.concurrent.Semaphore;
  7. import java.util.concurrent.TimeUnit;
  8. import java.util.concurrent.atomic.AtomicInteger;
  9.  
  10. /**
  11.  * Quick comparison between synchronized, synchronized lock array, AtomicInteger array,
  12.  * and Semaphore array increment loops.
  13.  *
  14.  * My overall point in this is that 10 MILLION iterations show a difference. The unit cost
  15.  * of the synchronized block is negligible.
  16.  *
  17.  * @author graywatson
  18.  */
  19. public class IncrementLockingComparison {
  20.  
  21.     final int NUM_INTEGERS = 100000;
  22.     final int NUM_THREADS = 10;
  23.  
  24.     final int[] ints = new int[NUM_INTEGERS];
  25.     final AtomicInteger[] atomicIntegers = new AtomicInteger[ints.length];
  26.     final Semaphore[] semaphores = new Semaphore[ints.length];
  27.     final Object[] locks = new Object[ints.length];
  28.  
  29.     {
  30.         for (int i = 0; i < atomicIntegers.length; i++) {
  31.             atomicIntegers[i] = new AtomicInteger(0);
  32.         }
  33.         for (int i = 0; i < semaphores.length; i++) {
  34.             semaphores[i] = new Semaphore(1);
  35.         }
  36.         for (int i = 0; i < locks.length; i++) {
  37.             locks[i] = new Object();
  38.         }
  39.     }
  40.  
  41.     public static void main(String[] args) throws Exception {
  42.         IncrementLockingComparison foo = new IncrementLockingComparison();
  43.         foo.doSingleLockIncrement();
  44.         foo.doMultipleLockIncrement();
  45.         foo.doAtomicIncrement();
  46.         foo.doSemaphoreIncrement();
  47.     }
  48.  
  49.     private void doSingleLockIncrement() throws Exception {
  50.         long before = System.currentTimeMillis();
  51.         ExecutorService pool = Executors.newCachedThreadPool();
  52.         for (int i = 0; i < NUM_THREADS; i++) {
  53.             pool.submit(new SingleLockIncrement());
  54.         }
  55.         pool.shutdown();
  56.         pool.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
  57.         long after = System.currentTimeMillis();
  58.         System.out.println("Single lock = " + (after - before));
  59.     }
  60.  
  61.     private void doMultipleLockIncrement() throws Exception {
  62.         long before = System.currentTimeMillis();
  63.         ExecutorService pool = Executors.newCachedThreadPool();
  64.         for (int i = 0; i < NUM_THREADS; i++) {
  65.             pool.submit(new MultipleLockIncrement());
  66.         }
  67.         pool.shutdown();
  68.         pool.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
  69.         long after = System.currentTimeMillis();
  70.         System.out.println("Lock array = " + (after - before));
  71.     }
  72.  
  73.     private void doAtomicIncrement() throws Exception {
  74.         long before = System.currentTimeMillis();
  75.         ExecutorService pool = Executors.newCachedThreadPool();
  76.         for (int i = 0; i < NUM_THREADS; i++) {
  77.             pool.submit(new AtomicIncrement());
  78.         }
  79.         pool.shutdown();
  80.         pool.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
  81.         long after = System.currentTimeMillis();
  82.         System.out.println("AtomicInteger array = " + (after - before));
  83.     }
  84.  
  85.     private void doSemaphoreIncrement() throws Exception {
  86.         long before = System.currentTimeMillis();
  87.         ExecutorService pool = Executors.newCachedThreadPool();
  88.         for (int i = 0; i < NUM_THREADS; i++) {
  89.             pool.submit(new SemaphoreIncrement());
  90.         }
  91.         pool.shutdown();
  92.         pool.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
  93.         long after = System.currentTimeMillis();
  94.         System.out.println("Semaphore array = " + (after - before));
  95.     }
  96.  
  97.     private void doLocksIncrement() throws Exception {
  98.         long before = System.currentTimeMillis();
  99.         ExecutorService pool = Executors.newCachedThreadPool();
  100.         for (int i = 0; i < NUM_THREADS; i++) {
  101.             pool.submit(new MultipleLockIncrement());
  102.         }
  103.         pool.shutdown();
  104.         pool.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
  105.         long after = System.currentTimeMillis();
  106.         System.out.println(after - before);
  107.     }
  108.  
  109.     private class SingleLockIncrement implements Runnable {
  110.         @Override
  111.         public void run() {
  112.             Random random = new Random();
  113.             for (int i = 0; i < 10000000; i++) {
  114.                 synchronized (ints) {
  115.                     ints[random.nextInt(ints.length)]++;
  116.                 }
  117.             }
  118.         }
  119.     }
  120.  
  121.     private class MultipleLockIncrement implements Runnable {
  122.         @Override
  123.         public void run() {
  124.             Random random = new Random();
  125.             assert ints.length == locks.length;
  126.             for (int i = 0; i < 10000000; i++) {
  127.                 int which = random.nextInt(ints.length);
  128.                 synchronized (locks[which]) {
  129.                     ints[which]++;
  130.                 }
  131.             }
  132.         }
  133.     }
  134.  
  135.     private class AtomicIncrement implements Runnable {
  136.         @Override
  137.         public void run() {
  138.             Random random = new Random();
  139.             for (int i = 0; i < 10000000; i++) {
  140.                 atomicIntegers[random.nextInt(atomicIntegers.length)].incrementAndGet();
  141.             }
  142.         }
  143.     }
  144.  
  145.     private class SemaphoreIncrement implements Runnable {
  146.         @Override
  147.         public void run() {
  148.             Random random = new Random();
  149.             assert ints.length == semaphores.length;
  150.             for (int i = 0; i < 10000000; i++) {
  151.                 int which = random.nextInt(ints.length);
  152.                 try {
  153.                     semaphores[which].acquire();
  154.                 } catch (InterruptedException e) {
  155.                     return;
  156.                 }
  157.                 try {
  158.                     ints[which]++;
  159.                 } finally {
  160.                     semaphores[which].release();
  161.                 }
  162.             }
  163.         }
  164.     }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement