Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Threads vs Instances  RSS feed

 
Shankar Narayana
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I was studying regarding synchronization and i am bit confused about the terms Threads, processes, and instances. My confusion is we use synchronization when multiple threads try to access a method, now if the method is an instance method does it really need to be synchronized. I have a case where each request is creating a new object and that object has a method which is getting a unique value from the database, now do i need to synchronize this method or should i make that method static and then synchronize.

I hope you people are getting my point, I appreciate your responses very much.


Regards,
Shankar.
 
Stephen Huey
Ranch Hand
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What kind of request? What is generating your different threads? It is possible that two threads could be accessing the same method on the same instance/object. Maybe you should post the relevant bits of your code...
 
Jeffrey Spaulding
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you use the term request...

If you're in a Servlet environment things are different again.

The Servlet itself is always multithreaded per request, so every object
you instantiate might very well be overwritten by the other thread
from the other request - if you place its declaration in a servlet scope.

Please explain a bit how your environment looks.

J.
 
Kishan Kumar
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Synchronisation makes sense only when two or more threads are acting on the same instance. When a new object is always getting created for each request then definetely there is no need to synchronise as different threads act of different instances. However you should be very sure that the instance created is always new and not taken from a pool of instances.

Hope this helps.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to Threads and Synchronization...
 
Shankar Narayana
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you use the term request...

If you're in a Servlet environment things are different again.

The Servlet itself is always multithreaded per request, so every object
you instantiate might very well be overwritten by the other thread
from the other request - if you place its declaration in a servlet scope.

Please explain a bit how your environment looks.


I am indeed in the Servlet environment, what do you mean by overwritten by other thread, can you please provide with an example. My setup is almost similar to MVC architecture, each request sends 2 parameters (requestType and beanType) and depending upon these 2 parameters a new bean instance is created and I have a method in one of the beans which actually gets the next sequence number from the database, which is used as primary key. Now since the primary key generation should be unique, I should synchronize this method, it means it should be a static method or can it be instance method. If it is an instance method synchronization isn't really helpful right?.


Thanks,
Shankar.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The servlet container makes one instance of each servlet class. When two or more requests come in at the same time they both execute methods on the single instance on their own threads. If you use all local variables within methods you're safe. If you use instance variables (or other shared resources) they will be shared by all threads. This is probably not what you intend, but if it is you have to be careful to synchronize access to them.

We get sequence numbers from a database as you described. The database does a nice job of making this operation thread safe so you don't really have to. If you do any other updates be sure to put all such operations in transactions. Read up on isolation levels to decide how careful you have to be.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!