Skip to content

Commit

Permalink
Make BoundedStatsDeque threadsafe.
Browse files Browse the repository at this point in the history
Patch by brandonwilliams, reviewed by jbellis for CASSANDRA-4019
  • Loading branch information
driftx committed Mar 8, 2012
1 parent f239907 commit d096471
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions src/java/org/apache/cassandra/utils/BoundedStatsDeque.java
Expand Up @@ -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()
Expand All @@ -50,12 +49,19 @@ public void clear()
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);
}
}

0 comments on commit d096471

Please sign in to comment.