cassandra-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jbel...@apache.org
Subject [36/44] git commit: Make BoundedStatsDeque threadsafe. Patch by brandonwilliams, reviewed by jbellis for CASSANDRA-4019
Date Mon, 02 Apr 2012 17:27:18 GMT
Make BoundedStatsDeque threadsafe.
Patch by brandonwilliams, reviewed by jbellis for CASSANDRA-4019


Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo
Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/28a9a15e
Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/28a9a15e
Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/28a9a15e

Branch: refs/heads/cassandra-1.1.0
Commit: 28a9a15e1cbc2422d56ed28fc6fa7093328c7764
Parents: c0a342b
Author: Brandon Williams <brandonwilliams@apache.org>
Authored: Thu Mar 8 17:32:43 2012 -0600
Committer: Brandon Williams <brandonwilliams@apache.org>
Committed: Thu Mar 8 17:32:43 2012 -0600

----------------------------------------------------------------------
 .../apache/cassandra/utils/BoundedStatsDeque.java  |   26 +++++++++------
 1 files changed, 16 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cassandra/blob/28a9a15e/src/java/org/apache/cassandra/utils/BoundedStatsDeque.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/utils/BoundedStatsDeque.java b/src/java/org/apache/cassandra/utils/BoundedStatsDeque.java
index 3256564..5d27608 100644
--- a/src/java/org/apache/cassandra/utils/BoundedStatsDeque.java
+++ b/src/java/org/apache/cassandra/utils/BoundedStatsDeque.java
@@ -18,21 +18,20 @@
 */
 package org.apache.cassandra.utils;
 
-import java.util.ArrayDeque;
 import java.util.Iterator;
+import java.util.NoSuchElementException;
+import java.util.concurrent.LinkedBlockingDeque;
 
 /**
- * not threadsafe.  caller is responsible for any locking necessary.
+ * bounded threadsafe deque
  */
 public class BoundedStatsDeque extends AbstractStatsDeque
 {
-    private final int size;
-    protected final ArrayDeque<Double> deque;
+    protected final LinkedBlockingDeque<Double> deque;
 
     public BoundedStatsDeque(int size)
     {
-        this.size = size;
-        deque = new ArrayDeque<Double>(size);
+        deque = new LinkedBlockingDeque<Double>(size);
     }
 
     public Iterator<Double> iterator()
@@ -50,12 +49,19 @@ public class BoundedStatsDeque extends AbstractStatsDeque
         deque.clear();
     }
 
-    public void add(double o)
+    public void add(double i)
     {
-        if (size == deque.size())
+        if (!deque.offer(i))
         {
-            deque.remove();
+            try
+            {
+                deque.remove();
+            }
+            catch (NoSuchElementException e)
+            {
+                // oops, clear() beat us to it
+            }
+            deque.offer(i);
         }
-        deque.add(o);
     }
 }


Mime
View raw message