activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cshan...@apache.org
Subject [activemq] branch master updated: AMQ-7300 - Improve performance of LongSequenceGenerator
Date Fri, 06 Sep 2019 15:57:05 GMT
This is an automated email from the ASF dual-hosted git repository.

cshannon pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/activemq.git


The following commit(s) were added to refs/heads/master by this push:
     new 4db2656  AMQ-7300 - Improve performance of LongSequenceGenerator
4db2656 is described below

commit 4db26560660783c5a7b15af7ca9ba27c9f4d04bc
Author: Christopher L. Shannon (cshannon) <christopher.l.shannon@gmail.com>
AuthorDate: Fri Sep 6 11:55:02 2019 -0400

    AMQ-7300 - Improve performance of LongSequenceGenerator
    
    LongSequenceGenerator now uses an AtomicLongFieldUpdater instead of
    synchronized
---
 .../activemq/util/LongSequenceGenerator.java       | 25 +++++++++++++++-------
 1 file changed, 17 insertions(+), 8 deletions(-)

diff --git a/activemq-client/src/main/java/org/apache/activemq/util/LongSequenceGenerator.java
b/activemq-client/src/main/java/org/apache/activemq/util/LongSequenceGenerator.java
index 53ae7f1..f3590a9 100644
--- a/activemq-client/src/main/java/org/apache/activemq/util/LongSequenceGenerator.java
+++ b/activemq-client/src/main/java/org/apache/activemq/util/LongSequenceGenerator.java
@@ -16,20 +16,29 @@
  */
 package org.apache.activemq.util;
 
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.atomic.AtomicLongFieldUpdater;
+
+/**
+ * Updated LongSequenceGenerator that uses an AtomicLongFieldUpdater for performance instead
of
+ * synchronized methods
+ */
 public class LongSequenceGenerator {
 
-    private long lastSequenceId;
+    private static final AtomicLongFieldUpdater<LongSequenceGenerator> SEQUENCE_UPDATER
=
+            AtomicLongFieldUpdater.newUpdater(LongSequenceGenerator.class, "lastSequenceId");
 
-    public synchronized long getNextSequenceId() {
-        return ++lastSequenceId;
-    }
+    private volatile long lastSequenceId;
 
-    public synchronized long getLastSequenceId() {
-        return lastSequenceId;
+    public long getNextSequenceId() {
+        return SEQUENCE_UPDATER.incrementAndGet(this);
     }
 
-    public synchronized void setLastSequenceId(long l) {
-        lastSequenceId = l;
+    public long getLastSequenceId() {
+        return SEQUENCE_UPDATER.get(this);
     }
 
+    public void setLastSequenceId(long l) {
+        SEQUENCE_UPDATER.set(this, l);
+    }
 }


Mime
View raw message