aspose file tools
The moose likes Threads and Synchronization and the fly likes Confused about thread safety Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "Confused about thread safety" Watch "Confused about thread safety" New topic
Author

Confused about thread safety

Miroslav Krivoklat
Greenhorn

Joined: Aug 15, 2006
Posts: 2
I think I'm misunderstanding something very fundamental about method synchronization and thread safety. Here's some example code from Java's String class about what I'm not grasping. Notice that the toUpperCase method is public and NOT synchronized. It calls the upperCaseIndex method which is also not synchronized but IS static. So, this doesn't seem safe to me. Meaning, couldn't two instances of String have the method toUpperCase called, and wouldn't they be in danger of both executing the upperCaseIndex method at the same time, overwriting each other's local variables?


public String toUpperCase(Locale loc)
{
...
...
if (expand > 0)
{
int index = upperCaseIndex(ch);
while (expand-- >= 0)
newStr[j++] = upperExpand[index++];
}
...
...
}


private static int upperCaseIndex(char ch)
{
// Simple binary search for the correct character.
int low = 0;
int hi = upperSpecial.length - 2;
int mid = ((low + hi) >> 2) << 1;
char c = upperSpecial[mid];
while (ch != c)
{
if (ch < c)
hi = mid - 2;
else
low = mid + 2;
mid = ((low + hi) >> 2) << 1;
c = upperSpecial[mid];
}
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

Local variables are not local to the method, they are local to the method invocation. When the thread finishes invoking the method, those variables disappear. When another thread invokes the method, it gets its own set of variables. Two threads can not communicate by sharing 'local' variables because they can not see each others local variables.
Miroslav Krivoklat
Greenhorn

Joined: Aug 15, 2006
Posts: 2
OK - that makes sense to me now. Thanks much...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Confused about thread safety
 
Similar Threads
Created application for the change pattern AAAA AAAB AABA AABB ect
urgent please help...look at code
To Hex String and back...
More help on java modifications
Converting Infix to Postfix Expressions