• Post Reply Bookmark Topic Watch Topic
  • New Topic

question on syncronization  RSS feed

 
raj malhotra
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I have one singleton class.This class can be accessed by many client browsers.These clients can access different methods of the class or same method at a given time.My question is do i need to use synchronized block in the method say for the code which is creating a file?
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If all clients access the same file, then yes. Is there one file for all clients, or can there be one file for each client? if the latter, you could make the client ID part of the file name, and not have to worry about synchronization.
 
raj malhotra
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is only one java class accessed by many clients and that class is singleton.If my java class is singleton ,do is still need to synchronize the code in it?This class basically do operations on files and directories,it create ,modify ,delete them.This application is used to maintain a set of files and directories.Different clients will create different folder but under common folder.
[ October 06, 2008: Message edited by: raj malhotra ]
 
Srikanth Basa
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From your statement it appears that you don't need synchronization but going a bit further to confirm it.

In the singleton class, do you preserve any client specific information in the member fields ?
 
raj malhotra
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No i don't save any client specific information in member's field.In singleton class pattern code , i found synchronized block in used whenever you instantiate or create a resource.Is this the only reason we should use synchronized block in the singleton class?
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to worry about synchronization when you're dealing with shared mutable state, for example a file in this case. If each client has its own file/directory, then you should be OK. If, on the other hand, more than one client accesses any particular file, then you do need to synchronize.
 
raj malhotra
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ulf, I got it.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!