java - HashMap.toString() 方法的线程安全

标签 java thread-safety hashmap tostring

我有以下代码:

public Hashmap<String,String> tempmap = new HashMap<String,String>();

和一个类函数:

void f1(){
    synchronized(onClassVariable1){
    ....Some code onClassVariable1...
    String tempString = tempMap.toString();
    ... Some work onClassVarible1 and tempString ...
    }
  }

多线程通过这个类的一个对象调用这个函数f1..

这个函数是线程安全的吗??

最佳答案

这取决于是否有方法在onClassVariable1上不同步并修改tempMap

关于java - HashMap.toString() 方法的线程安全,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10009367/

相关文章:

java - 未实现未连接的套接字

java - 在 Java 中返回 Map.Entry

c++ - 将 (string, object * ) 插入哈希表 (C++)

ios - 在嵌套 block 中保留对象的正确方法是什么

java - HashMap:它在TextView中显示最小值,但min_variable有更多值

java - 通过 Windows 用户 ID 在 Java 中进行即时消息传递?

java - 当新阶段弹出时,我如何禁用初级阶段

java - 更改窗口大小和 JLabel 的奇怪行为时如何更改字符串长度(以像素为单位计算其宽度)

java - 在这种情况下如何有效地使用线程?

c++ - MFC 和线程。跳过文档/ View 并制作我自己的解决方案?