一图流

原子类 - 图1

代码示例

升级类原子类

  1. public static class Person {
  2. // 1. 要求 升级类原子类可以访问到
  3. // 2. 要求有 volatile 修饰
  4. public volatile Integer integerScore;
  5. public volatile int basicScore;
  6. public Person(Integer integerScore, int basicScore) {
  7. this.integerScore = integerScore;
  8. this.basicScore = basicScore;
  9. }
  10. @Override
  11. public String toString() {
  12. return "Person{" +
  13. "score1=" + integerScore +
  14. ", score2=" + basicScore +
  15. '}';
  16. }
  17. }
  18. public static void main(String[] args) throws InterruptedException {
  19. // 1.1 将类的引用属性原子化
  20. // 1.2 <类class, 属性class>
  21. // TODO 待测试
  22. AtomicReferenceFieldUpdater<Person, Integer> integerScoreUpdater =
  23. AtomicReferenceFieldUpdater.newUpdater(
  24. Person.class, // 要升级的引用属性所属类 class
  25. Integer.class, // 要升级的引用属性 class
  26. "integerScore" // 要升级的属性名
  27. );
  28. // 2.1 将类的Integer属性原子化
  29. // 2.2 <类class>
  30. AtomicIntegerFieldUpdater<Person> BasicScoreUpdater =
  31. AtomicIntegerFieldUpdater.newUpdater(
  32. Person.class, // 要升级的 Integer 类型的属性所属类 class
  33. "basicScore" // 要升级的 Integer 类型的属性名
  34. );
  35. // 用于线程不安全操作
  36. Person danger = new Person(0, 0);
  37. // 用于线程安全操作
  38. Person safe = new Person(0, 0);
  39. Runnable updateScore = new Runnable() {
  40. @Override
  41. public void run() {
  42. // 基本类型
  43. danger.basicScore++;
  44. BasicScoreUpdater.getAndIncrement(safe);
  45. }
  46. };
  47. Thread[] threads = new Thread[10000];
  48. for (int i = 0; i < 10000; i++) {
  49. threads[i] = new Thread(updateScore);
  50. }
  51. for (int i = 0; i < 10000; i++) {
  52. threads[i].start();
  53. }
  54. for (int i = 0; i < 10000; i++) {
  55. threads[i].join();
  56. }
  57. System.out.println("danger: " + danger);
  58. System.out.println("safe: " + safe);
  59. }
  • 输出
    1. danger: Person{score1=0, score2=9995}
    2. safe: Person{score1=0, score2=10000}

adder 累加器

  1. private static AtomicLong atomicLong = new AtomicLong(0);
  2. private static LongAdder adderLong = new LongAdder();
  3. private static Runnable atomicR = () -> {
  4. int times = 10000;
  5. for (int i = 0; i < times; i++) {
  6. atomicLong.getAndIncrement();
  7. }
  8. };
  9. private static Runnable adderR = () -> {
  10. int times = 10000;
  11. for (int i = 0; i < times; i++) {
  12. adderLong.increment();
  13. }
  14. };
  15. private static long testAtomic(int taskCount, Runnable runnable) {
  16. ExecutorService service = Executors.newFixedThreadPool(16);
  17. long start = System.currentTimeMillis();
  18. for (int i = 0; i < taskCount; i++) {
  19. service.submit(runnable);
  20. }
  21. service.shutdown();
  22. // 等待线程池任务执行完毕
  23. while (!service.isTerminated()) {
  24. }
  25. long end = System.currentTimeMillis();
  26. return end - start;
  27. }
  28. public static void main(String[] args) {
  29. int taskCount = 10000;
  30. long atomicResult = testAtomic(taskCount, atomicR);
  31. long adderResult = testAtomic(taskCount, adderR);
  32. // 1. use 1715 mills
  33. System.out.println("AtomicLong useTime: " + atomicResult + " count: " + atomicLong.get());
  34. System.out.println("LongAdder useTime: " + adderResult + " count: " + adderLong.sum());
  35. }