Created
October 25, 2016 14:28
-
-
Save makasprzak/6b5d223938ca0d5ceffc9976a680a65d to your computer and use it in GitHub Desktop.
with reordering and queue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.goeuro.logic.search; | |
import com.jayway.awaitility.Awaitility; | |
import lombok.Builder; | |
import lombok.Data; | |
import lombok.Value; | |
import org.junit.Test; | |
import java.util.Random; | |
import java.util.concurrent.*; | |
import java.util.concurrent.atomic.AtomicInteger; | |
import java.util.concurrent.atomic.AtomicReference; | |
public class AtomicTest { | |
public static final int EXEC_TIMES = 10_000; | |
@Test | |
public void threadUnsafe() throws Exception { | |
Random random = new Random(System.currentTimeMillis()); | |
AtomicReference<UnsafePojo> reference = new AtomicReference<>(new UnsafePojo()); | |
AtomicInteger inconsistenciesCounter = new AtomicInteger(0); | |
AtomicInteger execCounter = new AtomicInteger(EXEC_TIMES); | |
ThreadPoolExecutor executorService = new ThreadPoolExecutor(4, 4, 1, TimeUnit.MINUTES, new LinkedBlockingQueue<>(10_000)); | |
executorService.submit(() -> { | |
for (int i = 0; i < 1_000_000; i++) { | |
if (random.nextBoolean()) { | |
reference.getAndUpdate(ref -> { | |
ref.setX(ref.getX() + 1); | |
return ref; | |
}); //in single thread difference between x and y should never be | |
// greater then 1 | |
reference.getAndUpdate(ref -> { | |
ref.setY(ref.getY() + 1); | |
return ref; | |
}); | |
} else { | |
reference.getAndUpdate(ref -> { | |
ref.setY(ref.getY() + 1); | |
return ref; | |
}); | |
reference.getAndUpdate(ref -> { | |
ref.setX(ref.getX() + 1); | |
return ref; | |
}); //in single thread difference between x and y should never be | |
// greater then 1 | |
} | |
} | |
}); | |
for (int i = 0; i < 10_000; i++) { | |
executorService.submit(() -> { | |
UnsafePojo ref = reference.get(); | |
if (Math.abs(ref.getX() - ref.getY()) > 1) | |
inconsistenciesCounter.incrementAndGet(); | |
execCounter.decrementAndGet(); | |
}); | |
} | |
Awaitility.await().atMost(5, TimeUnit.MINUTES).until(() -> execCounter.get() <= 0); | |
System.out.println("MUTABLE - Nr of inconsistencies detected: " + inconsistenciesCounter.get()); | |
} | |
@Test | |
public void threadSafe() throws Exception { | |
Random random = new Random(System.currentTimeMillis()); | |
AtomicReference<ImmutablePojo> reference = new AtomicReference<>(ImmutablePojo.builder().build()); | |
AtomicInteger inconsistenciesCounter = new AtomicInteger(0); | |
AtomicInteger execCounter = new AtomicInteger(EXEC_TIMES); | |
ThreadPoolExecutor executorService = new ThreadPoolExecutor(4, 4, 1, TimeUnit.MINUTES, new LinkedBlockingQueue<>(10_000)); | |
executorService.submit(() -> { | |
for (int i = 0; i < 1_000_000; i++) { | |
if (random.nextBoolean()) { | |
reference.getAndUpdate(ref -> ref.toBuilder().x(ref.getX() + 1).build()); //in single thread difference between x and y should never be | |
// greater then 1 | |
reference.getAndUpdate(ref -> ref.toBuilder().y(ref.getY() + 1).build()); | |
} else { | |
reference.getAndUpdate(ref -> ref.toBuilder().y(ref.getY() + 1).build()); | |
reference.getAndUpdate(ref -> ref.toBuilder().x(ref.getX() + 1).build()); //in single thread difference between x and y should never be | |
// greater then 1 | |
} | |
} | |
}); | |
for (int i = 0; i < 10_000; i++) { | |
executorService.submit(() -> { | |
ImmutablePojo ref = reference.get(); | |
if (Math.abs(ref.getX() - ref.getY()) > 1) | |
inconsistenciesCounter.incrementAndGet(); | |
execCounter.decrementAndGet(); | |
}); | |
} | |
Awaitility.await().atMost(5, TimeUnit.MINUTES).until(() -> execCounter.get() <= 0); | |
System.out.println("IMMUTABLE - Nr of inconsistencies detected: " + inconsistenciesCounter.get()); | |
} | |
} | |
@Value | |
@Builder(toBuilder = true) | |
class ImmutablePojo { | |
int x; | |
int y; | |
} | |
@Data | |
class UnsafePojo { | |
int x; | |
int y; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment