This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

one method, two threads  RSS feed

 
Peter Kleczka
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose I have a single object and two threads calling the same unsynchronized method on that object. If the method operates only
local variables I imagine that the method would behave as if
it were synchronized. Is this correct?

The context of this question is in trying to understand declarations
in a jsp page. If I write, for instance:

two different JSP's calling this at the same time
won't get the results from the other JSP's arguments
passed to this method.

TIA for your answers.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it "behaved as if it were synchronized", this would mean that one call would have to wait for the other; that's not the case here. But yes, the two threads will not interfere with each other at all; both can call the method at once with no ill effects.
 
Sol Mayer-Orn
Ranch Hand
Posts: 311
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a little addition to the (very correct) reply above:
The reason *why* the 2 threads won't interfer with each other, is because method's local variables (including parameters) are placed on a "stack", and each thread has its own stack.
So if you have 2 threads, each of them has its own copy of 'x', 'y' and any locally-declared method parameters.

However, note that if your parameter were non-primitive (e.g.: public int myMethod( ArrayList x )) then you're *not* on safe grounds, because only the reference is copied into the stack, and 'x' data is still shared.
[ March 15, 2007: Message edited by: Sol Mam-Orn ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!