org.cliffc.high_scale_lib
Class NonBlockingHashSet<E>

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractSet<E>
          extended by org.cliffc.high_scale_lib.NonBlockingHashSet<E>
All Implemented Interfaces:
Serializable, Iterable<E>, Collection<E>, Set<E>

public class NonBlockingHashSet<E>
extends AbstractSet<E>
implements Serializable

A simple wrapper around NonBlockingHashMap making it implement the Set interface. All operations are Non-Blocking and multi-thread safe.

Since:
1.5
Author:
Cliff Click
See Also:
Serialized Form

Constructor Summary
NonBlockingHashSet()
          Make a new empty NonBlockingHashSet.
 
Method Summary
 boolean add(E o)
          Add o to the set.
 void clear()
          Empty the set.
 boolean contains(Object o)
           
 Iterator<E> iterator()
           
 void readOnly()
          Atomically make the set immutable.
 boolean remove(Object o)
          Remove o from the set.
 int size()
          Current count of elements in the set.
 
Methods inherited from class java.util.AbstractSet
equals, hashCode, removeAll
 
Methods inherited from class java.util.AbstractCollection
addAll, containsAll, isEmpty, retainAll, toArray, toArray, toString
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Set
addAll, containsAll, isEmpty, retainAll, toArray, toArray
 

Constructor Detail

NonBlockingHashSet

public NonBlockingHashSet()
Make a new empty NonBlockingHashSet.

Method Detail

add

public boolean add(E o)
Add o to the set.

Specified by:
add in interface Collection<E>
Specified by:
add in interface Set<E>
Overrides:
add in class AbstractCollection<E>
Returns:
true if o was added to the set, false if o was already in the set.

contains

public boolean contains(Object o)
Specified by:
contains in interface Collection<E>
Specified by:
contains in interface Set<E>
Overrides:
contains in class AbstractCollection<E>
Returns:
true if o is in the set.

remove

public boolean remove(Object o)
Remove o from the set.

Specified by:
remove in interface Collection<E>
Specified by:
remove in interface Set<E>
Overrides:
remove in class AbstractCollection<E>
Returns:
true if o was removed to the set, false if o was not in the set.

size

public int size()
Current count of elements in the set. Due to concurrent racing updates, the size is only ever approximate. Updates due to the calling thread are immediately visible to calling thread.

Specified by:
size in interface Collection<E>
Specified by:
size in interface Set<E>
Specified by:
size in class AbstractCollection<E>
Returns:
count of elements.

clear

public void clear()
Empty the set.

Specified by:
clear in interface Collection<E>
Specified by:
clear in interface Set<E>
Overrides:
clear in class AbstractCollection<E>

iterator

public Iterator<E> iterator()
Specified by:
iterator in interface Iterable<E>
Specified by:
iterator in interface Collection<E>
Specified by:
iterator in interface Set<E>
Specified by:
iterator in class AbstractCollection<E>

readOnly

public void readOnly()
Atomically make the set immutable. Future calls to mutate will throw an IllegalStateException. Existing mutator calls in other threads racing with this thread and will either throw IllegalStateException or their update will be visible to this thread. This implies that a simple flag cannot make the Set immutable, because a late-arriving update in another thread might see immutable flag not set yet, then mutate the Set after the readOnly() call returns. This call can be called concurrently (and indeed until the operation completes, all calls on the Set from any thread either complete normally or end up calling readOnly() internally).

This call is useful in debugging multi-threaded programs where the Set is constructed in parallel, but construction completes after some time; and after construction the Set is only read. Making the Set read-only will cause updates arriving after construction is supposedly complete to throw an IllegalStateException.