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?

[4428 byte] By [java_swing_dudea] at [2007-11-27 11:36:17]
# 1

if (ticksBlockingQueue.getName().equals("TickQ1")) {

q1TicksAddedtoDP.incrementAndGet();

} else if (ticksBlockingQueue.getName().equals("TickQ2")) {

q2TicksAddedtoDP.getAndIncrement();

} else if (ticksBlockingQueue.getName().equals("TickQ3")) {

q3TicksAddedtoDP.getAndIncrement();

} else if (ticksBlockingQueue.getName().equals("TickQ4")) {

q4TicksAddedtoDP.getAndIncrement();

} else if (ticksBlockingQueue.getName().equals("TickQ5")) {

q5TicksAddedtoDP.getAndIncrement();

} else if (ticksBlockingQueue.getName().equals("TickQ6")) {

q6TicksAddedtoDP.getAndIncrement();

} else if (ticksBlockingQueue.getName().equals("TickQ7")) {

q7TicksAddedtoDP.getAndIncrement();

}

Something does not look right with the above, what would you do if you had 100 qxTicksAddedtoDP's? Are these all the "qxTicksAddedtoDP's" classes that you have?

I don't really understand your other problem but I bet it is not down to the method being synchronized.

_helloWorld_a at 2007-7-29 17:07:54 > top of Java-index,Java Essentials,Java Programming...
# 2

This is hard-coded for the moment. My problem is with the sybcronization between the addAll and the calculator locking the dirtyPositioSet while it is calculating. How can I decouple this to spped it up is the question.... Thanks

java_swing_dudea at 2007-7-29 17:07:54 > top of Java-index,Java Essentials,Java Programming...
# 3

Iterate over a copy of the queue.

ejpa at 2007-7-29 17:07:54 > top of Java-index,Java Essentials,Java Programming...