org.amino.utility
Class AdaptEliminationArray

java.lang.Object
  extended by org.amino.utility.AdaptEliminationArray
All Implemented Interfaces:
IEliminationArray

public class AdaptEliminationArray
extends java.lang.Object
implements IEliminationArray

This class implements similar functionality of EliminationArray. And this class try to get some intelligence by adjusting size of elimination array automatically. If elimination fails frequently for two reasons, we will adjust size of elimination array:

  1. When finding match operation, frequently got empty slot from elimination array
  2. CAS operation frequently fails
For the 1st reason, we should decrement size of elimination array. For the 2nd reason, we should enlarge elimination array. And this is the main logic of this class. FIXME: Now this class only count succ and fail, we should identify different reasons of failure

Author:
Zhi Gan (ganzhi@gmail.com)

Constructor Summary
AdaptEliminationArray(int arraySize)
          Create elimination array with specified size.
 
Method Summary
 void dump()
          dump for debug.
 boolean tryAdd(java.lang.Object obj, int backOff)
          Try to add element without touching the central data structure.
 java.lang.Object tryRemove(int backOff)
          Try to remove element without touching central data structure.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AdaptEliminationArray

public AdaptEliminationArray(int arraySize)
Create elimination array with specified size.

Parameters:
arraySize - the average size of internal array. Size of internal array will vary between 1 and 2*arraySize
Method Detail

dump

public void dump()
dump for debug.


tryAdd

public boolean tryAdd(java.lang.Object obj,
                      int backOff)
               throws java.lang.InterruptedException
Try to add element without touching the central data structure. If this operation can successfully locate a removing thread, it will succeed. Else, it will sleep for several milliseconds and waiting to be located by removing threads.

Specified by:
tryAdd in interface IEliminationArray
Parameters:
obj - the adding object
backOff - time in millisecond for sleeping if match haven't been found immediately.
Returns:
true if match happened between this method and tryRemove(int)
Throws:
java.lang.InterruptedException - throw exception if interrupted

tryRemove

public java.lang.Object tryRemove(int backOff)
                           throws java.lang.InterruptedException
Try to remove element without touching central data structure. If this operation can successfully locate a adding thread, it will succeed. Else, it will sleep for several milliseconds and waiting to be located by adding threads.

Specified by:
tryRemove in interface IEliminationArray
Parameters:
backOff - time in millisecond for sleeping if match haven't been found immediately.
Returns:
null if no match. Argument to tryAdd() method if successful match
Throws:
java.lang.InterruptedException - throw exception if be interrupted


Copyright © 2008. All Rights Reserved.