This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes static methods synchronization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "static methods synchronization" Watch "static methods synchronization" New topic
Author

static methods synchronization

Saikat Mukherjee
Greenhorn

Joined: Mar 06, 2008
Posts: 13

Just a general question .

Suppose that I have a method called add().



There is no synchronization mechanism implemented.

Now if I have say a thousand live threads that call this method, is there a possibility that I could get a result of say add(2,2) = 5(or anything else other than 4).

I have not been able to replicate this anyhow. But I think that this is absolutely possible.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Saikat Mukherjee wrote:But I think that this is absolutely possible.

Why do you think that?

No, it is not possible. Every time the method is called, there are two new variables a and b. If multiple threads call the method at the same time, each thread has its own copy of a and b, which doesn't interfere with the variables of other threads.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Saikat Mukherjee
Greenhorn

Joined: Mar 06, 2008
Posts: 13
Thanks for the quick reply.
Really appreciate it
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static methods synchronization
 
Similar Threads
Thread synchronization ..... CIW
Do I need synchronization here ?
synchronizing on a class vs an instance of a class
Problems with threads, PLEASE IGNORE MY PREVIOUS TOPICS
Program on Threads