Multithreading ConcurrentHashMap and Lists
Hello all,
I apologise for re-posting.
I have a data structure as follows
ConcurrentHashMap<String, Object>
The Object corresponds to 3 lists
List<Long>, List<Long>, List<Long>
These long's correspond to file pointer positions
and the string is a file name.
i.e. each file has a series of long 'positions' from where
objects will be read/modified and written back.
This ConcurrentHashMap will be accessed by multiple threads,
and objects from the file will be read/modified and written back.
There is another set of threads which write data to those files.
(if a file is created for the first time, then a new entry will be
put in the ConcurrentHashMap)
Right now - my pseudo - code looks something like this
////////THIS GENERIC MAP CAN BE ACCESSED BY ALL THREADS AND STORES ALL DATA
////////
public ConcurrentHashMap<String, ListObjects> GenericMapStructure;
////////
////////
//Threads that writes data to file uses this function
////////
public synchronized boolean writeSMS(Object object, String fileName){
//
//Do the write process
//Update the list values
//Update the ConcurrentHashMap
GenericMapStructure.put(fileName, listObjects);
//return a boolean
//
}
////////
//Threads that read/modify the log values and update the concurrent hash map
////////
//This thread will be called multiple times by passing the file name via the //thread class constructor
private ConcurrentHashMap<String, ListObjects> concurrentMap;
public void run(){
for(;;){
//Get a local copy of the GenericMapStructure
concurrentMap = GenericMapStructure;
if(FILENAME IS AVAILABLE IN THE CONCURRENTHASHMAP ){
//GET THE LIST OBJECTS FROM THE CONCURRENTHASHMAP
//EXTRACT LISTS FROM THAT OBJECT
//ITERATE OVER THE LIST
Iterator activeIterator = smsList.iterator();
//MANIPULTAE THE OBJECTS FROM THE FILE;
//UPDATE THE LIST with LONGS & PUT IT BACK IN THE ACTUAL GENERIC QUEUE
GenericMapStructure.put(String.valueOf(keyIS), listObjects);
}
else{
Thread.sleep(100);
}
}
}
The problem is the API states that ConcurrentHashMap is thread safe, however
Retrievals reflect the results of the most recently completed update operations holding upon their onset.
So imagine my writeSMS method writes
1 long value into the 1st List<Long>
2 long values into the 2nd List<Long>
3 long values into the 3rd List<Long>
& updates it in the Map.
Then read/modify threads starts accessing the Map and
starts changing the Long values in my lists; i.e. I am going over the Iterator (smsList.iterator())
At this point is there a possiblity for my writeSMS method to write new values into the smsList
of the corresponding Key (filename)?
If that happens then my Map will gert corrupted, i.e. i dont want to write something into the
MAP when I am reading/modifying the list values.
I am having a local copy of the GenericMapStructure for each thread, since there will be multiple threads accessing different keys of ther MAP and I dont want the threads to wait for one-another!
In short - is my pseudo-code thread safe?
best regards.

