简体   繁体   中英

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.

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

该方法不需要同步,因为它没有副作用,即它不涉及任何类/实例字段,也不处理任何对象。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM