Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2016
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.31 KB | None | 0 0
  1. import com.google.common.cache.CacheBuilder;
  2. import com.google.common.cache.CacheLoader;
  3. import com.google.common.cache.LoadingCache;
  4.  
  5. import java.util.HashSet;
  6. import java.util.Map;
  7. import java.util.Set;
  8. import java.util.concurrent.ConcurrentHashMap;
  9. import java.util.concurrent.ConcurrentMap;
  10. import java.util.concurrent.atomic.AtomicInteger;
  11.  
  12. public class ConcurrencyTest {
  13.  
  14.     private static final int THREAD_COUNT = Runtime.getRuntime().availableProcessors() + 1;
  15.  
  16.     /**
  17.      * Should be smaller than {@link ConcurrencyTest#THREAD_COUNT}, preferably about 1/4
  18.      */
  19.     private static final int KEY_COUNT = THREAD_COUNT / 4;
  20.  
  21.     private static String getKeyFor(int i) {
  22.         return "key" + (i % KEY_COUNT);
  23.     }
  24.  
  25.     /**
  26.      * Should not create any output
  27.      */
  28.     static class ComputeIfAbsentTest {
  29.         private static final ConcurrentMap<String, TestObject> map = new ConcurrentHashMap<>();
  30.  
  31.         public static void main(String[] args) {
  32.             while (true) {
  33.                 AtomicInteger counter = new AtomicInteger(0);
  34.  
  35.                 Set<Thread> threads = new HashSet<>();
  36.                 for (int i = 0; i < THREAD_COUNT; i++) {
  37.                     int finalI = i;
  38.                     Thread thread = new Thread(() -> map.computeIfAbsent(getKeyFor(finalI),
  39.                             s -> new TestObject(counter)));
  40.                     threads.add(thread);
  41.                 }
  42.  
  43.                 for (Thread thread : threads) {
  44.                     thread.start();
  45.                 }
  46.  
  47.                 for (Thread thread : threads) {
  48.                     try {
  49.                         thread.join();
  50.                     } catch (InterruptedException ignored) {
  51.                     }
  52.                 }
  53.  
  54.                 int count = counter.get();
  55.                 if (count != KEY_COUNT) {
  56.                     System.out.println("Expected " + KEY_COUNT + " but got " + count);
  57.                 }
  58.                 map.clear();
  59.             }
  60.         }
  61.     }
  62.  
  63.     /**
  64.      * Should not create any output
  65.      */
  66.     static class LoadingCacheTest {
  67.         private static final AtomicInteger counter = new AtomicInteger(0);
  68.  
  69.         private static final LoadingCache<String, TestObject> cache = CacheBuilder.newBuilder()
  70.                 .build(new CacheLoader<String, TestObject>() {
  71.                     @Override
  72.                     @SuppressWarnings("NullableProblems")
  73.                     public TestObject load(String key) throws Exception {
  74.                         return new TestObject(counter);
  75.                     }
  76.                 });
  77.  
  78.         public static void main(String[] args) {
  79.             while (true) {
  80.                 Set<Thread> threads = new HashSet<>();
  81.                 for (int i = 0; i < THREAD_COUNT; i++) {
  82.                     int finalI = i;
  83.                     Thread thread = new Thread(() -> cache.getUnchecked(getKeyFor(finalI)));
  84.                     threads.add(thread);
  85.                 }
  86.  
  87.                 for (Thread thread : threads) {
  88.                     thread.start();
  89.                 }
  90.  
  91.                 for (Thread thread : threads) {
  92.                     try {
  93.                         thread.join();
  94.                     } catch (InterruptedException ignored) {
  95.                     }
  96.                 }
  97.  
  98.                 int count = counter.get();
  99.                 if (count != KEY_COUNT) {
  100.                     System.out.println("Expected " + KEY_COUNT + " but got " + count);
  101.                 }
  102.                 cache.invalidateAll();
  103.                 counter.set(0);
  104.             }
  105.  
  106.         }
  107.     }
  108.  
  109.     /**
  110.      * Should not create any output
  111.      */
  112.     static class PutIfAbsentSynchronizedTest {
  113.         private static final Map<String, TestObject> map = new ConcurrentHashMap<>();
  114.  
  115.         public static void main(String[] args) {
  116.             while (true) {
  117.                 AtomicInteger counter = new AtomicInteger(0);
  118.  
  119.                 Set<Thread> threads = new HashSet<>();
  120.                 for (int i = 0; i < THREAD_COUNT; i++) {
  121.                     int finalI = i;
  122.                     Thread thread = new Thread(() -> addToMapAndGetTestObject(getKeyFor(finalI), counter));
  123.                     threads.add(thread);
  124.                 }
  125.  
  126.                 for (Thread thread : threads) {
  127.                     thread.start();
  128.                 }
  129.  
  130.                 for (Thread thread : threads) {
  131.                     try {
  132.                         thread.join();
  133.                     } catch (InterruptedException ignored) {
  134.                     }
  135.                 }
  136.  
  137.                 int count = counter.get();
  138.                 if (count != KEY_COUNT) {
  139.                     System.out.println("Expected " + KEY_COUNT + " but got " + count);
  140.                 }
  141.                 map.clear();
  142.             }
  143.         }
  144.  
  145.         private static TestObject addToMapAndGetTestObject(String key, AtomicInteger counter) {
  146.             TestObject existing = map.get(key);
  147.             if (existing != null) {
  148.                 return existing;
  149.             } else {
  150.                 synchronized (map) {
  151.                     TestObject existingSynchronized = map.get(key);
  152.                     if (existingSynchronized != null) {
  153.                         return existingSynchronized;
  154.                     } else {
  155.                         TestObject object = new TestObject(counter);
  156.                         map.putIfAbsent(key, object);
  157.                         return map.get(key);
  158.                     }
  159.                 }
  160.             }
  161.         }
  162.     }
  163.  
  164.     /**
  165.      * Will create some output since some extra objects will be created
  166.      */
  167.     static class PutIfAbsentNonSynchronizedTest {
  168.         private static final Map<String, TestObject> map = new ConcurrentHashMap<>();
  169.  
  170.         public static void main(String[] args) {
  171.             while (true) {
  172.                 AtomicInteger counter = new AtomicInteger(0);
  173.  
  174.                 Set<Thread> threads = new HashSet<>();
  175.                 for (int i = 0; i < THREAD_COUNT; i++) {
  176.                     int finalI = i;
  177.                     Thread thread = new Thread(() -> addToMapAndGetTestObject(getKeyFor(finalI), counter));
  178.                     threads.add(thread);
  179.                 }
  180.  
  181.                 for (Thread thread : threads) {
  182.                     thread.start();
  183.                 }
  184.  
  185.                 for (Thread thread : threads) {
  186.                     try {
  187.                         thread.join();
  188.                     } catch (InterruptedException ignored) {
  189.                     }
  190.                 }
  191.  
  192.                 int count = counter.get();
  193.                 if (count != KEY_COUNT) {
  194.                     System.out.println("Expected " + KEY_COUNT + " but got " + count);
  195.                 }
  196.  
  197.                 map.clear();
  198.             }
  199.         }
  200.  
  201.         private static TestObject addToMapAndGetTestObject(String key, AtomicInteger counter) {
  202.             TestObject existing = map.get(key);
  203.             if (existing != null) {
  204.                 return existing;
  205.             } else {
  206.                 TestObject object = new TestObject(counter);
  207.                 map.putIfAbsent(key, object);
  208.                 return map.get(key);
  209.             }
  210.         }
  211.     }
  212.  
  213.     private static class TestObject {
  214.         TestObject(AtomicInteger counter) {
  215.             counter.incrementAndGet();
  216.         }
  217.     }
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement