Thread Pool

Hi,

I have a HashMap storing objects of type Employee. This map is keyed by employee id. I have a data feed sending me employee updates at a rate of up to 200 to 300 employee updates persecond.

What I need to do is get the employee id from the data feed and check if it is in my Employee HashMap. If it is I need to update the entry if not I need to add a new emploee entry to the HashMap. Simple enough so far!!

Now, When these updates com in I have to perform calculations on the Employee objects in the Employee Hash map up date the employee object and store it back into the HashMap. I want to do this using threads to spped up the processing.

I have a recalculate method on my employee object which does the calculations required.

How best can I perform this functionality concurrently?

I am reading the data feed in a thread and storing these values in an updatedEmploee HashMap. I was thinking of using the Executors to perform the calculations in a thread. SHould I do this for each employee object or all the employee objects in the updatedEmployee HashMap?

ExecutorService execSvc = Executors.newCachedThreadPool();

for (int j = 0; j < numberOfUpdatedEmployees; j++){

Employees emp = (Employee) employeeMap.get(empID);

execSvc.execute(new Calculator(pos) );

}

execSvc.shutdown();

class Calculatorimplements Runnable{

Employee emp;

public Calculator(Employee emp){

this.emp = position;

publicvoid run(){

employee.recalculate();

employeeMap.put(empID, employee);

}

}

Am I thinking along the right lines? Can anyone suggest the best approach to take for this particular problem?

Thank you so much for your time on this.

[2228 byte] By [java_swing_dudea] at [2007-11-27 9:04:19]
# 1

Well, firstly, HashMap is not thread-safe. That is, it's not safe to access entries in the map from separate threads without some form of synchronization.

Look at ConcurrentMap (an interface implemented by ConcurrentHashMap among others) and the putIfAbsent method which is a thread-safe check-and-update method.

oxbow_lakesa at 2007-7-12 21:37:19 > top of Java-index,Core,Core APIs...
# 2
Thanks. Can anyone comment on the best approach to take for this problem?
java_swing_dudea at 2007-7-12 21:37:19 > top of Java-index,Core,Core APIs...