Skip to content

Instantly share code, notes, and snippets.

@neuecc
Created December 23, 2014 17:14
Show Gist options
  • Save neuecc/ac773bff6a38a3b7248e to your computer and use it in GitHub Desktop.
Save neuecc/ac773bff6a38a3b7248e to your computer and use it in GitHub Desktop.
Parallel.StringBuilder
using System.Linq;
using System.Text;
using System.Threading.Tasks;
class Program
{
static void Main(string[] args)
{
var result = new StringBuilder();
Parallel.ForEach(Enumerable.Range(1, 1000000), () => new StringBuilder(), (x, option, sb) =>
{
sb.Append(x);
return sb;
}, sb =>
{
lock (result)
{
result.Append(sb.ToString());
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment