Assume I have one threadpool and each thread is running following method:
void runMe(HashMap myHash){
...
myHash.remove(keyToBeRemoved);
...
}
My question is; should not myHash
be the same in all threads at the beginning? Because my second thread does not have the key keyToBeRemoved
. I was wondering why.
The myHash
reference may be the same for all threads, but when the first thread executes
myHash.remove(keyToBeRemoved);
then the hash map (which all references refer to) will no longer have that mapping.
HashMap hm => Depends on your program. If you are using it shared among threads, it will be modified by each thread simultaneously and you have provide concurrency.
keyToBeRemoved => same as above
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.