simpleguy
simpleguy

Reputation: 1

Does the method need to be synchronized?

I need to decide if the following method requires synchronization or not in a multi-threaded environment and why?

public class MultiMain 
{

 public int add(int a,int b)
 {

  int r=a+b;
  return r;
 }

}

I am new to multi-threading.I do not feel there is any need for synchronization there is no shared resource here but I am not sure about it.

Thanks in advance.

Upvotes: 0

Views: 170

Answers (2)

khachik
khachik

Reputation: 28703

No synchronization is needed for that method, because it doesn't have side-effects, i.e. it doesn't touch any class/instance field, and doesn't deal with any object.

Upvotes: 3

mch
mch

Reputation: 7373

No, there is not need to synchronize that method. There is no shared state between threads, so it is thread-safe.

Upvotes: 6

Related Questions