What does “ StringBuilders are not thread-safe” mean?

前端 未结 5 1823
攒了一身酷
攒了一身酷 2020-12-31 12:51

I have read some articles about the pros and cons of String and StringBuilder in the Java Programming language. In one of the articles, the author

5条回答
  •  粉色の甜心
    2020-12-31 13:34

    StringBuilder inside a method would be safe.

    public void addProperty(String name, String value) {
        private StringBuilder sb = new StringBuilder("1=2");
    
            if (value != null && value.length() > 0) {
                if (sb.length() > 0) {
                    sb.append(',');
            }
            sb.append(name).append('=').append(value);
        }
    }
    

提交回复
热议问题