Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.google.common.cache.CacheBuilder;
- import com.google.common.cache.CacheLoader;
- import com.google.common.cache.LoadingCache;
- import java.util.HashSet;
- import java.util.Map;
- import java.util.Set;
- import java.util.concurrent.ConcurrentHashMap;
- import java.util.concurrent.ConcurrentMap;
- import java.util.concurrent.atomic.AtomicInteger;
- public class ConcurrencyTest {
- private static final int THREAD_COUNT = Runtime.getRuntime().availableProcessors() + 1;
- /**
- * Should be smaller than {@link ConcurrencyTest#THREAD_COUNT}, preferably about 1/4
- */
- private static final int KEY_COUNT = THREAD_COUNT / 4;
- private static String getKeyFor(int i) {
- return "key" + (i % KEY_COUNT);
- }
- /**
- * Should not create any output
- */
- static class ComputeIfAbsentTest {
- private static final ConcurrentMap<String, TestObject> map = new ConcurrentHashMap<>();
- public static void main(String[] args) {
- while (true) {
- AtomicInteger counter = new AtomicInteger(0);
- Set<Thread> threads = new HashSet<>();
- for (int i = 0; i < THREAD_COUNT; i++) {
- int finalI = i;
- Thread thread = new Thread(() -> map.computeIfAbsent(getKeyFor(finalI),
- s -> new TestObject(counter)));
- threads.add(thread);
- }
- for (Thread thread : threads) {
- thread.start();
- }
- for (Thread thread : threads) {
- try {
- thread.join();
- } catch (InterruptedException ignored) {
- }
- }
- int count = counter.get();
- if (count != KEY_COUNT) {
- System.out.println("Expected " + KEY_COUNT + " but got " + count);
- }
- map.clear();
- }
- }
- }
- /**
- * Should not create any output
- */
- static class LoadingCacheTest {
- private static final AtomicInteger counter = new AtomicInteger(0);
- private static final LoadingCache<String, TestObject> cache = CacheBuilder.newBuilder()
- .build(new CacheLoader<String, TestObject>() {
- @Override
- @SuppressWarnings("NullableProblems")
- public TestObject load(String key) throws Exception {
- return new TestObject(counter);
- }
- });
- public static void main(String[] args) {
- while (true) {
- Set<Thread> threads = new HashSet<>();
- for (int i = 0; i < THREAD_COUNT; i++) {
- int finalI = i;
- Thread thread = new Thread(() -> cache.getUnchecked(getKeyFor(finalI)));
- threads.add(thread);
- }
- for (Thread thread : threads) {
- thread.start();
- }
- for (Thread thread : threads) {
- try {
- thread.join();
- } catch (InterruptedException ignored) {
- }
- }
- int count = counter.get();
- if (count != KEY_COUNT) {
- System.out.println("Expected " + KEY_COUNT + " but got " + count);
- }
- cache.invalidateAll();
- counter.set(0);
- }
- }
- }
- /**
- * Should not create any output
- */
- static class PutIfAbsentSynchronizedTest {
- private static final Map<String, TestObject> map = new ConcurrentHashMap<>();
- public static void main(String[] args) {
- while (true) {
- AtomicInteger counter = new AtomicInteger(0);
- Set<Thread> threads = new HashSet<>();
- for (int i = 0; i < THREAD_COUNT; i++) {
- int finalI = i;
- Thread thread = new Thread(() -> addToMapAndGetTestObject(getKeyFor(finalI), counter));
- threads.add(thread);
- }
- for (Thread thread : threads) {
- thread.start();
- }
- for (Thread thread : threads) {
- try {
- thread.join();
- } catch (InterruptedException ignored) {
- }
- }
- int count = counter.get();
- if (count != KEY_COUNT) {
- System.out.println("Expected " + KEY_COUNT + " but got " + count);
- }
- map.clear();
- }
- }
- private static TestObject addToMapAndGetTestObject(String key, AtomicInteger counter) {
- TestObject existing = map.get(key);
- if (existing != null) {
- return existing;
- } else {
- synchronized (map) {
- TestObject existingSynchronized = map.get(key);
- if (existingSynchronized != null) {
- return existingSynchronized;
- } else {
- TestObject object = new TestObject(counter);
- map.putIfAbsent(key, object);
- return map.get(key);
- }
- }
- }
- }
- }
- /**
- * Will create some output since some extra objects will be created
- */
- static class PutIfAbsentNonSynchronizedTest {
- private static final Map<String, TestObject> map = new ConcurrentHashMap<>();
- public static void main(String[] args) {
- while (true) {
- AtomicInteger counter = new AtomicInteger(0);
- Set<Thread> threads = new HashSet<>();
- for (int i = 0; i < THREAD_COUNT; i++) {
- int finalI = i;
- Thread thread = new Thread(() -> addToMapAndGetTestObject(getKeyFor(finalI), counter));
- threads.add(thread);
- }
- for (Thread thread : threads) {
- thread.start();
- }
- for (Thread thread : threads) {
- try {
- thread.join();
- } catch (InterruptedException ignored) {
- }
- }
- int count = counter.get();
- if (count != KEY_COUNT) {
- System.out.println("Expected " + KEY_COUNT + " but got " + count);
- }
- map.clear();
- }
- }
- private static TestObject addToMapAndGetTestObject(String key, AtomicInteger counter) {
- TestObject existing = map.get(key);
- if (existing != null) {
- return existing;
- } else {
- TestObject object = new TestObject(counter);
- map.putIfAbsent(key, object);
- return map.get(key);
- }
- }
- }
- private static class TestObject {
- TestObject(AtomicInteger counter) {
- counter.incrementAndGet();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement