I have an HashMap with key/value pair. The program processes each element of the hashMap, and should mark the HashMap element as "processed". i.e. I have the follwoing key/value pair.
It depends on what is being processed and what information you need to retain. Also, if your keys are just a sequenced index, you don't even need a Map implementation. A List would work fine. You can either mark the object processed or roll them off to another List of processed items.
I have ran a sample test and HashMap.put(..) does what I was looking for.
Basically, this is what my senario is:
I have a threaded application which spawns multiple threads, say 10. and each thread will operate on that HashMap list. So, soon as a thread chooses an element of the HashMap to operate on, it should mark the HashMap element as "Processed" so the other threads will not process the same element again. It's like simulating pasimestic locking mechanism.
Typically a Queue is used for that, often a BlockingQueue. Workers take items off the queue as they become available to process. Is there a reason that you need to leave them in a Map?
Last edited by Ezzaral; Oct 10th, 2008 at 4:46 pm.
Actually, no. There's no particular reason. This was my initial thaughts. To elaborate alittle more, here I give alittle more specific details.
I am operating on a very huge amount of data (somewhere arround 200,000 elements). I can use sequentally, but I want to reduce the amount of time the program will take.
The skeleton of my program looks something like this: I think this will give a clearer picture of what I am trying to acheive.
processElement(map.get(newInteger(j)).toString()); // this will the actual process
}
}
}
}
So, what I am asking, basically, will this designe work? in other words, will each thread operate exactly on on element of the Hashmap, which is not marked yet as "Processed"?
No, you have absolutely no synchronization there and will have all kinds of concurrency problems.
There are a lot of ways to structure what you are wanting to do, but you have to understand some basics about threading first. Additionally, if you are running on a single processor, starting 10 threads won't make it go faster - it will just have to time-slice the processing of each thread anyway.
Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.
This thread is more than three months old
No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.