Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

2 Synchronization blocks with same object

 
Manimekala Velautham
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have one Java class and it is a singleton class. I have one thread and one servlet.

The Java class has a
1. global variable called "myList" of type ArrayList to hold Hashmaps.
2. method addListValues()- Called by the thread every 1 hr to add a HashMap to the myList.
3. method processRequest()- Called by Servlet Requests to read the value from the myList to serve the requests.

In order to protect cuncurrent access to this myList variable, I have introduced synchronized blocks in the above methods. See the code below. Will this approach solve my problem?...Please help me with your views.

 
Edward Harned
Ranch Hand
Posts: 291
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try it? Does it work?

Don't post code here you haven't run yourself.

There are much better ways of doing this in the java.util.concurrent packages. Look into blocking queues, etc.
 
Ugender Rekulampally
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manimekala,

I don't think your synchronization in methods are correct.



Here why do you have myList.notifyAll()? I am assuming you wanted this method to be accessed by one thread at time and once it is done, that is it. you don't have to call notifyAll() to tell every other thread that accessing this method is done in this scenario. jvm automatically does that.

please look into wait() and notify()/notifyAll() concepts.

And same thing with other method



I think you should handle this logic in different way. you access this method if you have elements. so before you get here you make sure that one thread is accessing. if elements are not there, then wait() for some other thread fills in some elements in the list.

Thanks,
Ugender
 
Manimekala Velautham
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Thanks for your suggestions. I understood the purpose of wait() and notify().

In this, when the the processRequest method is in progress, I dont want to add anything to to myList shared variable. The Thread which is adding a new value or changes the value in particular index in myList should wait for the processRequest method to complete.

Same way, when the thread that is executing the addValue method is in progress on the myList variable, I want the requests to wait till the thread comes out from the addValue method.
 
Ugender Rekulampally
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manimekala,
Sure there may be different ways but is your program working as you expected? we only saw just two methods and didn't see how you are handling them or calling them.

Thanks,
Ugender
 
Manimekala Velautham
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

This is a servlet application. It has some HashMaps in memory, which contains key value pairs. These HashMaps are updated at every 1 hour by a thread. The servlet requests(https) will have the 'key' as query string and to fetch value from the hashmaps. The response will have the 'value'in XML format.

For the above logic, I have 3 Java Classses
1. QueryMap.java SERVLET CLASS
2. Manager.java JAVA CLASS
3. MapMonitor.java JAVA THREAD CLASS
Manager.java
-------------

In the above code,

the method processRequest() will be called by the servlet for each and every request to take the value from the hash map for the key requested.

the method addHashMap() will be called by the thread by every 1 hour to update or to place the new hashmaps in manager class. (maps are not going to be removed in this application)

My need is, If any request is processing, this thread has to wait for the processRequest method to complete. Same way, if the addHashMap() method is progress, the processRequest method has to wait utill it completes.

This code is working fine, but I am not able to figure out is there any problem of dead locks or other issues. Please review this code and suggest me whether my approah is correct or not.

Thanks
[/CODE]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic