feat: AtomicExample02
This commit is contained in:
parent
c51c07a7b8
commit
39d2b8b43f
|
@ -0,0 +1,82 @@
|
||||||
|
package cn.bunny.atomic;
|
||||||
|
|
||||||
|
import org.openjdk.jmh.annotations.*;
|
||||||
|
import org.openjdk.jmh.profile.StackProfiler;
|
||||||
|
import org.openjdk.jmh.runner.Runner;
|
||||||
|
import org.openjdk.jmh.runner.RunnerException;
|
||||||
|
import org.openjdk.jmh.runner.options.Options;
|
||||||
|
import org.openjdk.jmh.runner.options.OptionsBuilder;
|
||||||
|
import org.openjdk.jmh.runner.options.TimeValue;
|
||||||
|
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
import java.util.concurrent.locks.Lock;
|
||||||
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
|
|
||||||
|
@Measurement(iterations = 10)
|
||||||
|
@Warmup(iterations = 10)
|
||||||
|
@BenchmarkMode(Mode.AverageTime)
|
||||||
|
@OutputTimeUnit(TimeUnit.MICROSECONDS)
|
||||||
|
public class AtomicExample01 {
|
||||||
|
|
||||||
|
public static void main(String[] args) throws RunnerException {
|
||||||
|
Options options = new OptionsBuilder()
|
||||||
|
.include(AtomicExample01.class.getSimpleName())
|
||||||
|
.forks(1)
|
||||||
|
.timeout(TimeValue.seconds(10))
|
||||||
|
.addProfiler(StackProfiler.class)
|
||||||
|
.build();
|
||||||
|
new Runner(options).run();
|
||||||
|
}
|
||||||
|
|
||||||
|
@GroupThreads(10)
|
||||||
|
@Group("sync")
|
||||||
|
@Benchmark
|
||||||
|
public void syncInc(IntMonitor monitor) {
|
||||||
|
monitor.synInc();
|
||||||
|
}
|
||||||
|
|
||||||
|
@GroupThreads(10)
|
||||||
|
@Group("lock")
|
||||||
|
@Benchmark
|
||||||
|
public void lockInc(IntMonitor monitor) {
|
||||||
|
monitor.lockInc();
|
||||||
|
}
|
||||||
|
|
||||||
|
@GroupThreads(10)
|
||||||
|
@Group("atomic")
|
||||||
|
@Benchmark
|
||||||
|
public void atomicIntegerInc(AtomicIntegerMonitor monitor) {
|
||||||
|
monitor.inc();
|
||||||
|
}
|
||||||
|
|
||||||
|
@State(Scope.Group)
|
||||||
|
public static class IntMonitor {
|
||||||
|
private final Lock lock = new ReentrantLock();
|
||||||
|
private int x;
|
||||||
|
|
||||||
|
public void lockInc() {
|
||||||
|
lock.lock();
|
||||||
|
try {
|
||||||
|
x++;
|
||||||
|
} finally {
|
||||||
|
lock.unlock();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void synInc() {
|
||||||
|
synchronized (this) {
|
||||||
|
x++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@State(Scope.Group)
|
||||||
|
public static class AtomicIntegerMonitor {
|
||||||
|
private final AtomicInteger x = new AtomicInteger();
|
||||||
|
|
||||||
|
public void inc() {
|
||||||
|
x.incrementAndGet();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,43 @@
|
||||||
|
package cn.bunny.atomic;
|
||||||
|
|
||||||
|
import org.openjdk.jmh.annotations.*;
|
||||||
|
import org.openjdk.jmh.runner.Runner;
|
||||||
|
import org.openjdk.jmh.runner.RunnerException;
|
||||||
|
import org.openjdk.jmh.runner.options.Options;
|
||||||
|
import org.openjdk.jmh.runner.options.OptionsBuilder;
|
||||||
|
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
|
@Measurement(iterations = 10)
|
||||||
|
@Warmup(iterations = 10)
|
||||||
|
@BenchmarkMode(Mode.AverageTime)
|
||||||
|
@State(Scope.Thread)
|
||||||
|
@OutputTimeUnit(TimeUnit.MICROSECONDS)
|
||||||
|
public class AtomicExample02 {
|
||||||
|
|
||||||
|
private AtomicInteger atomicInteger;
|
||||||
|
|
||||||
|
public static void main(String[] args) throws RunnerException {
|
||||||
|
Options options = new OptionsBuilder()
|
||||||
|
.include(AtomicExample02.class.getSimpleName())
|
||||||
|
.forks(1)
|
||||||
|
.build();
|
||||||
|
new Runner(options).run();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Setup(Level.Iteration)
|
||||||
|
public void setup() {
|
||||||
|
this.atomicInteger = new AtomicInteger(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
public void testSet() {
|
||||||
|
this.atomicInteger.set(10);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
public void testLazySet() {
|
||||||
|
this.atomicInteger.lazySet(10);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,4 @@
|
||||||
|
package cn.bunny.atomic;
|
||||||
|
|
||||||
|
public class AtomicExample04 {
|
||||||
|
}
|
|
@ -0,0 +1,4 @@
|
||||||
|
package cn.bunny.atomic;
|
||||||
|
|
||||||
|
public class AtomicExample05 {
|
||||||
|
}
|
|
@ -0,0 +1,4 @@
|
||||||
|
package cn.bunny.atomic;
|
||||||
|
|
||||||
|
public class AtomicExample06 {
|
||||||
|
}
|
|
@ -0,0 +1,4 @@
|
||||||
|
package cn.bunny.atomic;
|
||||||
|
|
||||||
|
public class AtomicExample07 {
|
||||||
|
}
|
|
@ -0,0 +1,4 @@
|
||||||
|
package cn.bunny.atomic;
|
||||||
|
|
||||||
|
public class AtomicExample08 {
|
||||||
|
}
|
|
@ -0,0 +1,4 @@
|
||||||
|
package cn.bunny.atomic;
|
||||||
|
|
||||||
|
public class AtomicExample09 {
|
||||||
|
}
|
|
@ -0,0 +1,4 @@
|
||||||
|
package cn.bunny.atomic;
|
||||||
|
|
||||||
|
public class AtomicExample10 {
|
||||||
|
}
|
|
@ -0,0 +1,4 @@
|
||||||
|
package cn.bunny.atomic;
|
||||||
|
|
||||||
|
public class AtomicExample11 {
|
||||||
|
}
|
|
@ -0,0 +1,4 @@
|
||||||
|
package cn.bunny.atomic;
|
||||||
|
|
||||||
|
public class AtomicExample12 {
|
||||||
|
}
|
|
@ -0,0 +1,4 @@
|
||||||
|
package cn.bunny.atomic;
|
||||||
|
|
||||||
|
public class AtomicExample13 {
|
||||||
|
}
|
|
@ -0,0 +1,4 @@
|
||||||
|
package cn.bunny.atomic;
|
||||||
|
|
||||||
|
public class AtomicExample14 {
|
||||||
|
}
|
|
@ -0,0 +1,31 @@
|
||||||
|
package cn.bunny.atomic.reference;
|
||||||
|
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import static java.util.concurrent.ThreadLocalRandom.current;
|
||||||
|
|
||||||
|
public class AtomicExample03 {
|
||||||
|
static volatile DebitCard debitCard = new DebitCard("Bunny", 0);
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
for (int i = 0; i < 10; i++) {
|
||||||
|
new Thread("B-" + i) {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
while (true) {
|
||||||
|
final DebitCard dc = debitCard;
|
||||||
|
DebitCard newDc = new DebitCard(dc.getAccount(), debitCard.getAmount() + 10);
|
||||||
|
System.out.println(newDc);
|
||||||
|
|
||||||
|
debitCard = newDc;
|
||||||
|
try {
|
||||||
|
TimeUnit.MILLISECONDS.sleep(current().nextInt(20));
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}.start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,44 @@
|
||||||
|
package cn.bunny.atomic.reference;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
public class DebitCard {
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
private final String account;
|
||||||
|
@Getter
|
||||||
|
private final int amount;
|
||||||
|
|
||||||
|
public DebitCard(String account, int amount) {
|
||||||
|
this.account = account;
|
||||||
|
this.amount = amount;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a string representation of the object.
|
||||||
|
*
|
||||||
|
* @return a string representation of the object.
|
||||||
|
* @apiNote In general, the
|
||||||
|
* {@code toString} method returns a string that
|
||||||
|
* "textually represents" this object. The result should
|
||||||
|
* be a concise but informative representation that is easy for a
|
||||||
|
* person to read.
|
||||||
|
* It is recommended that all subclasses override this method.
|
||||||
|
* The string output is not necessarily stable over time or across
|
||||||
|
* JVM invocations.
|
||||||
|
* @implSpec The {@code toString} method for class {@code Object}
|
||||||
|
* returns a string consisting of the name of the class of which the
|
||||||
|
* object is an instance, the at-sign character `{@code @}', and
|
||||||
|
* the unsigned hexadecimal representation of the hash code of the
|
||||||
|
* object. In other words, this method returns a string equal to the
|
||||||
|
* value of:
|
||||||
|
* <blockquote>
|
||||||
|
* <pre>
|
||||||
|
* getClass().getName() + '@' + Integer.toHexString(hashCode())
|
||||||
|
* </pre></blockquote>
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "DebitCard{" + "account='" + account + '\'' + ", amount=" + amount + '}';
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue