Author: julien_viet
Date: 2011-06-09 17:24:49 -0400 (Thu, 09 Jun 2011)
New Revision: 6621
Modified:
components/common/trunk/common/src/main/java/org/gatein/common/concurrent/BoundedBuffer.java
components/common/trunk/common/src/main/java/org/gatein/common/concurrent/ObjectRef.java
Log:
GTNCOMMON-16 : Decrease synchronization in BoundedBuffer
Modified:
components/common/trunk/common/src/main/java/org/gatein/common/concurrent/BoundedBuffer.java
===================================================================
---
components/common/trunk/common/src/main/java/org/gatein/common/concurrent/BoundedBuffer.java 2011-06-09
11:35:59 UTC (rev 6620)
+++
components/common/trunk/common/src/main/java/org/gatein/common/concurrent/BoundedBuffer.java 2011-06-09
21:24:49 UTC (rev 6621)
@@ -60,7 +60,7 @@
private ObjectRef<T> first;
/** The size, it is declared as volatile for the @link{getSize()} method. */
- private volatile int size;
+ private int size;
public BoundedBuffer(int maxSize)
{
@@ -79,11 +79,6 @@
return maxSize;
}
- public int getSize()
- {
- return size;
- }
-
/**
* Add an element to the buffer.
*
@@ -91,26 +86,26 @@
*/
public void add(T t)
{
+ ObjectRef<T> added = new ObjectRef<T>(t);
synchronized (this)
{
if (first == null)
{
- first = new ObjectRef<T>(t);
- last = first;
+ last = first = added;
size = 1;
}
else
{
ObjectRef<T> tmp = first;
- first = new ObjectRef<T>(t);
- tmp.next.set(first);
+ first = added;
+ tmp.next = first;
if (size < maxSize)
{
size++;
}
else
{
- last = last.next.get();
+ last = last.next;
}
}
}
@@ -124,22 +119,20 @@
*/
public Iterator<T> iterator()
{
+ ObjectRef<T> last;
+ int size;
+ synchronized (this)
+ {
+ last = this.last;
+ size = this.size;
+ }
if (size == 0)
{
- List<T> empty = Collections.emptyList();
- return empty.iterator();
+ return Collections.<T>emptyList().iterator();
}
else
{
- // Get consistent state
- final ObjectRef<T> lastSnapshot;
- final int sizeSnapshot;
- synchronized (this)
- {
- lastSnapshot = last;
- sizeSnapshot = size;
- }
- return new BoundedIterator<T>(lastSnapshot, sizeSnapshot);
+ return new BoundedIterator<T>(last, size);
}
}
@@ -171,7 +164,7 @@
throw new NoSuchElementException();
}
T next = current.object;
- current = current.next.get();
+ current = current.next;
count++;
return next;
}
Modified:
components/common/trunk/common/src/main/java/org/gatein/common/concurrent/ObjectRef.java
===================================================================
---
components/common/trunk/common/src/main/java/org/gatein/common/concurrent/ObjectRef.java 2011-06-09
11:35:59 UTC (rev 6620)
+++
components/common/trunk/common/src/main/java/org/gatein/common/concurrent/ObjectRef.java 2011-06-09
21:24:49 UTC (rev 6621)
@@ -18,8 +18,6 @@
*/
package org.gatein.common.concurrent;
-import java.util.concurrent.atomic.AtomicReference;
-
/**
* An internal class to keep a reference onto another object, it is used in the bounded
buffer implementation.
*
@@ -33,11 +31,11 @@
final T object;
/** . */
- final AtomicReference<ObjectRef<T>> next;
+ volatile ObjectRef<T> next;
ObjectRef(T object)
{
this.object = object;
- this.next = new AtomicReference<ObjectRef<T>>();
+ this.next = null;
}
}
Show replies by date