
{"id":1060,"date":"2012-08-22T14:58:01","date_gmt":"2012-08-22T18:58:01","guid":{"rendered":"http:\/\/www.ikriv.com\/blog\/?p=1060"},"modified":"2012-08-22T14:58:01","modified_gmt":"2012-08-22T18:58:01","slug":"plinqs-asparallel-makes-concurrent-calls-to-underlying-ienumerable","status":"publish","type":"post","link":"https:\/\/ikriv.com\/blog\/?p=1060","title":{"rendered":"PLinq&#039;s AsParallel() makes concurrent calls to underlying IEnumerable"},"content":{"rendered":"<p>I am trying to play with PLINQ, and I was wondering, if I have an enumerable that, say, reads lines from a file, or stock prices off Yahoo Finance, would AsParallel() call it from one thread or from multiple threads concurrently. Experiment shows that it&#8217;s the latter:<\/p>\n<pre><code>class Program\n{\n    [ThreadStatic]\n    static int Index = 0;\n\n    static IEnumerable<string> GetStrings()\n    {\n        while (true)\n        {\n            yield return String.Format(\"Thread {0} String {1}\", Thread.CurrentThread.ManagedThreadId, ++Index);\n        }\n    }\n\n    static void Main(string[] args)\n    {\n        using (var writer = new ParallelTextWriter(\"out{0}.txt\"))\n        {\n            GetStrings().AsParallel().Take(1000).ForAll(s => { writer.WriteLine(s); });\n        }\n    }\n}<\/code><\/pre>\n<p>The output is<\/p>\n<p><code>Thread 1 String 1<br \/>\nThread 1 String 2<br \/>\n...<br \/>\nThread 1 String 800<br \/>\nThread 3 String 1<br \/>\nThread 3 String 2<br \/>\n...<br \/>\nThread 3 String 200<\/code><\/p>\n<p>Obviously, the ratio of strings between threads (800\/200 or 500\/500) could be different each time.<br \/>\n<a style=\"display:none\" href=\"http:\/\/www.codeproject.com\/script\/Articles\/BlogFeedList.aspx?amid=1181663\" rel=\"tag\">CodeProject<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I am trying to play with PLINQ, and I was wondering, if I have an enumerable that, say, reads lines from a file, or stock prices off Yahoo Finance, would <a href=\"https:\/\/ikriv.com\/blog\/?p=1060\" class=\"more-link\">[&hellip;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"Layout":"","footnotes":""},"categories":[3,8,16,4],"tags":[],"class_list":["entry","author-ikriv","post-1060","post","type-post","status-publish","format-standard","category-dotnet","category-cs","category-demos","category-hack"],"_links":{"self":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1060","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1060"}],"version-history":[{"count":0,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1060\/revisions"}],"wp:attachment":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1060"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1060"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1060"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}