Locking Performance
Hi...
I have a design in which I have multiple threads reading objects from a queue and passing an attribute of this object to a set which represents the set of objects that need to be calculated.
I then have a seperate calculator thread which runs every two seconds and recalculates these objects.
Iterator iterator = positionMap.keySet().iterator();
// Loop through the positions and add them to the update set.
while (iterator.hasNext()){
//TODO Possible optimisation remove the declaration to outside the while.
String key = (String)iterator.next();
Pos position = positionMap.get(key);
if (position !=null){
dirtyPositions.add(key);
}else{
System.out.println("Null Position");
}
}
synchronized(dirtyPositionSet){
dirtyPositionSet.addAll(dirtyPositions);
if (ticksBlockingQueue.getName().equals("TickQ1")){
q1TicksAddedtoDP.incrementAndGet();
}elseif (ticksBlockingQueue.getName().equals("TickQ2")){
q2TicksAddedtoDP.getAndIncrement();
}elseif (ticksBlockingQueue.getName().equals("TickQ3")){
q3TicksAddedtoDP.getAndIncrement();
}elseif (ticksBlockingQueue.getName().equals("TickQ4")){
q4TicksAddedtoDP.getAndIncrement();
}elseif (ticksBlockingQueue.getName().equals("TickQ5")){
q5TicksAddedtoDP.getAndIncrement();
}elseif (ticksBlockingQueue.getName().equals("TickQ6")){
q6TicksAddedtoDP.getAndIncrement();
}elseif (ticksBlockingQueue.getName().equals("TickQ7")){
q7TicksAddedtoDP.getAndIncrement();
}
ticksAdded.getAndIncrement();
}
}else{
ticksIgnored.incrementAndGet();
}
The calculator is as follows
// Lock the Dirty Positions Set
synchronized (dirtyPositionSet){
System.out.println("");
System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
System.out.println("Calculation set holds " + dirtyPositionSet.size() +" positions.");
System.out.println("");
Iterator iter = dirtyPositionSet.iterator();
while (iter.hasNext()){
String key = (String) iter.next();
positionsToCalculate.add(key);
}
// Clear all
dirtyPositionSet.clear();
}// Unlock Dirty Positions Set
The problem is that when I am trying to process alot of records my queue is backing up due to the synchronization. How can I speed this up?

