• Post Reply Bookmark Topic Watch Topic
  • New Topic

Threads and Methods without Synchronisation  RSS feed

 
Simon Harvey
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I'm tryin to figure out how threads pass through methods without interfering with each other.
Bill Brogden kindly answered a previosu question related to this by saying that each thread gets its own instance of the method to play with.
Is that how threads usually work? I thought a thread could get paused halfway through a method and then another thread could come through and cock things up? Is that not right? And thats why we have the synchronised keyword - to make sure that another thread - with access to all the same values - doesnt take, manipulate and replace values written by another thread.
I just dont get how say 50 simultaneous users dont mess up with each others data if all fifty of them are stomping about the same instance! Where does synchronisation come into it?
Alot of that examples I have seen have made short references to the need for synchronisation, but then very few examples actually have any synchronisation at all. They maybe show a token example straight out of the java tutorial.
I guess my question is:
With so many possible requests all flying about the place, and with lots of methods doing things - where does synchronisation fit into it all? When do I need to synchronise and went do I not need to? All these threads are freaking me out!!! Waaaahh
Thanks everyone. This is a great site and servlets are really cool.
Take care
Simon
 
Michael Zalewski
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, it would be more correct to think of each thread getting it's own instance of the method data (not the method).
So take for example the two following 'Hello World' type servlets, that take name= query parameter.

This does not have a problem because the variable name is inside the method. Each request is a seperate thread, and each thread gets its own copy of the variable name.
But if its rewritten this way:

It is no longer correct. There is only one copy of the servlet object (type BadServlet). So all threads share only one copy of the String object name.
It could happen that two threads will execute the statement marked //1 before either thread executes the statement at //2. If it happens that way, the name printed on both responses will come from the last thread which passed through //1. Both requests will display the same response, even if the requests had different values for the name parameter.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sun has a pretty good Thread Tutorial at java.sun.com.
The essential point about Threads here is that each one has its own chunk of memory for keeping its own copy of primitives and references that are involved in calling methods and using local variables that are created inside a method.
Thus, in a servlet situation, each Thread that is handling a request has its own reference to a unique request object, its own reference to a unique respons object, etc... Setting those up is handled by the servlet engine before your servlet gets called. No other Thread can see them.
Synchronization only comes into play when more than one Thread can see/use the same object.
Bill
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!