2019年12月15日 星期日

StringBuilder搭配lock達成Thread-Safe

C#沒有StringBuffer這個Thread-Safe 物件
還是可以透過搭配lock達成Thread-Safe 



https://www.sololearn.com/Discuss/657740/can-i-use-a-stringbuilder-in-a-parallel-foreachloop

lock(sb) { sb.Append(fruit).Append("!!").Append(fruit); }

C#
https://github.com/imrexhuang/HelloThreadStatic/blob/master/HelloThreadStatic/HelloThreadStatic/Program.cs 

 Java
https://gist.github.com/imrexhuang/df799e10868fed6d7ec94a7129160691 
 
http://developer.classpath.org/doc/java/lang/StringBuffer-source.html
 182:   public synchronized int length()
 183:   {
 184:     return count;
 185:   }
 
 
http://developer.classpath.org/doc/java/lang/StringBuilder-source.html
 173:   public int length()
 174:   {
 175:     return count;
 176:   } 
 

沒有留言:

張貼留言