• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

synchronizedHashMap

 
Ranch Hand
Posts: 137
Hibernate Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Am using a synchronized HashMap to keep live connections for reuse. For some reason i find more than one thread get access to a connection object from the map (conns.get) and get in to a BLOCKED state as per below thread dump. Here first thread "A-worker-pool-40-thread-1" get a lock on "0x00000007518b7020" and then "A-worker-pool-40-thread-5" and "A-worker-pool-40-thread-6" wait for the same lock "0x00000007518b7020" to get released. Am i misinterpreting something here? Please suggest.

"A-worker-pool-40-thread-1" prio=10 tid=0x00007fa27800e000 nid=0xd6b0 runnable [0x00007f21dffc8000]
at com.xyz.connection.getConnection(Connection.java)
- locked <0x00000007518b7020> (a com.xyz.Connection)
       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
       at java.lang.Thread.run(Thread.java:745)

"A-worker-pool-40-thread-5" prio=10 tid=0x00007fa498001800 nid=0xd6af waiting for monitor entry [0x00007f225ffca000]
java.lang.Thread.State: BLOCKED (on object monitor)
at com.xyz.connection.getConnection(Connection.java)
- waiting to lock <0x00000007518b7020> (a com.xyz.Connection)
       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
       at java.lang.Thread.run(Thread.java:745)

"A-worker-pool-40-thread-6"  prio=10 tid=0x00007fa9d800e800 nid=0xd6ae waiting for monitor entry [0x00007f22dffcb000]
java.lang.Thread.State: BLOCKED (on object monitor)
at com.xyz.connection.getConnection(Connection.java)
- waiting to lock <0x00000007518b7020> (a com.xyz.Connection)
       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
       at java.lang.Thread.run(Thread.java:745)

 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The way I read those locks, is that they are blocked calling the synchronized method getConnection()

That would indicate they have tried to look up the connection in the conns map, and have not found anything - so call conn.getConnection().
Whether things are actually blocked or not would depend on your getConnection method implementation.

A synchronized map will guarantee that you will always have a consistent map to deal with.  i.e. you can use the Map from multiple threads, and expect it to return consistent information.
However it won't guarantee anything about how many threads can retrieve a reference from the Map.  You would need some sort of locking/semaphore logic for that .



 
Sidharth Pallai
Ranch Hand
Posts: 137
Hibernate Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stefan Evans wrote:
However it won't guarantee anything about how many threads can retrieve a reference from the Map.  You would need some sort of locking/semaphore logic for that.



Hi Evans,

Does it mean that 2 threads can get the same connection object upon synchronizedMap.get(String Key) ?
Java API reads all the methods of synchronizedMap are mutex as below:


public V get(Object key) {
           synchronized (mutex) {return m.get(key);}

}
 
author
Posts: 23912
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sidharth Pallai wrote:
Does it mean that 2 threads can get the same connection object upon synchronizedMap.get(String Key) ?
Java API reads all the methods of synchronizedMap are mutex as below:

public V get(Object key) {
           synchronized (mutex) {return m.get(key);}

}



The purpose of the mutex is to prevent the simultaneous retrieval of the key, in order to prevent the corruption of the hashmap. However, once the key has been retrieved, it doesn't protect against simultaneous usage of the key object.

So, if the key is a connection object, the mutex should be able to help give the same connection to 2 threads without corrupting the hashmap. It doesn't prevent 2 threads from getting the same connection -- you will need a separate lock for that.

Henry
reply
    Bookmark Topic Watch Topic
  • New Topic