• Post Reply Bookmark Topic Watch Topic
  • New Topic

static methods synchronization

 
Saikat Mukherjee
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 15767
74
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Saikat Mukherjee
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the quick reply.
Really appreciate it
 
my overalls have superpowers - they repel people who think fashion is important. Tiny ad:
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!