简体   繁体   English

方法是否需要同步?

[英]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. 线程之间没有共享状态,因此它是线程安全的。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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