0

Now I have a HashMap called words and a Producer class that extends Thread class.

And I have a for loop like that:

for (int i = 0; i < 10; i++) {
  Producer producer = new Producer(i);
  producer.run();
}

I want to let each producer add some words into the HashMap words. Does it work if I just put the HashMap<...> words about the for (int....) statement?

It not, what should I do to achieve this?

Bin Zhu
  • 201
  • 1
  • 2
  • 6
  • 1
    You need to synchronize access to the HashMap or use a ConcurrentMap otherwise it could break if multiple threads update it. – Thilo Jul 19 '13 at 05:14
  • 2
    Also, if your `Producer` extends `Thread` you need to call `start` (not `run`) to actually cause another thread to be created. – SimonC Jul 19 '13 at 05:16
  • @Thilo couldn't he just add the hashmap to a monitor object and have all access to it synchronized or is that not enough? (just curious) – Martin Larsson Jul 19 '13 at 05:17
  • @MartinLarsson that's exactly what `Collections.synchronizedMap` does. It's enough, while (potentially) performance-impacting. – alf Jul 19 '13 at 05:22
  • 1
    `Producer.run()` does not cause multiple threads to run. You are executing all this code in the same thread. You need to call `start(),` not `run().` – user207421 Jul 19 '13 at 05:34

2 Answers2

1

I'm afraid you don't specify the question precisely enough for it to be answered.

Does it work if I just put the HashMap<...> words about the for (int....) statement?

It almost does; that is, it will indeed create a new hash map (provided that "the words" are right). What it won't do is it won't make sure that has map is synchronized. As @Thilo says in the comment above, there are two easy ways to do so:

  1. ConcurrentHashMap provides you with an easy multithreading-ready map implementation
  2. Collections.synchronizedMap wraps a map of your choice making it ready for a multithreaded environment

Pros and cons of the approaches (and way more) can be found at What's the difference between ConcurrentHashMap and Collections.synchronizedMap(Map)?

Community
  • 1
  • 1
alf
  • 8,377
  • 24
  • 45
0

The ConcurrentHashMap is very similar to the HashMap class, except that ConcurrentHashMap offers internally maintained concurrency. It means you do not need to have synchronized blocks when accessing ConcurrentHashMap in multithreaded application.

//Initialize ConcurrentHashMap instance
ConcurrentHashMap<String, Integer> m = new ConcurrentHashMap<String, Integer>();

//Print all values stored in ConcurrentHashMap instance
for each (Entry<String, Integer> e : m.entrySet())
{
system.out.println(e.getKey()+"="+e.getValue());
}

Above code is reasonably valid in multi-threaded environment in your application. The reason, I am saying “reasonably valid” is that, above code yet provides thread safety, still it can decrease the performance of application. And ConcurrentHashMap was introduced to improve the performance while ensuring thread safety.

To improve it's performace you can adjust following parameters as per your need:

  • initialCapacity

  • loadFactor

  • concurrencyLevel
Ankur Lathi
  • 7,636
  • 5
  • 37
  • 49