Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question About Synchronization

 
Chetan Parekh
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an object in application scope. Object contains cached data and iterate the object [][].

I am getting the reference of the object in many jsp files and utilizing data from it.

At any given point of time, there will be at least 20 references of the object.

Now my question is, since there will be a single Object in an application scope, but having 20 references of the same - when I execute any method of the object (let’s say at a time, from 5 places I am executing the same method), will it be a synchronized call or asynchronized?
 
Rohit Bhagwat
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Chetan Parekh:

Now my question is, since there will be a single Object in an application scope, but having 20 references of the same - when I execute any method of the object (let’s say at a time, from 5 places I am executing the same method), will it be a synchronized call or asynchronized?


HI Chetan

As per the best practices only those objects should be kept in application scope that are going to be read only. Which means the state of an object which is stored in application scope is not going to change. So the synchronization is solved.

If you are modifying the state of an object then you have to first obtain the lock on that object so that nobody uses it till the state is changed. And since you are changing the state of object so even if there are 20 or 30 references to the same object the changed state is reflected in every case.
 
Chetan Parekh
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rohit for your reply.

My object will be contact object. It will generate and kept in application scope when server boots, after that it will never changed.

In object, I have an object [][] (will not change once it is created ) and methods iterate thru array and return values.

My worry is when multiple jsp files are iterating thru the same object [][]. Will it be asynchronized?
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Chetan Parekh:
My object will be contact object. It will generate and kept in application scope when server boots, after that it will never changed.

In object, I have an object [][] (will not change once it is created ) and methods iterate thru array and return values.

My worry is when multiple jsp files are iterating thru the same object [][]. Will it be asynchronized?


Yes. But doesn't matter since it is read-only.
 
Chetan Parekh
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's great!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic