Parallel and PLINQ: parallelplinq

Source: Internet
Author: User

Parallel and PLINQ: parallelplinq

Parallel is a simplified Task usage. It contains three static methods: Parallel. For, Parallel. ForEach, and Parallel. Invoke. PLINQ is a parallel version of LINQ. The following are simple examples and precautions for Parallel and PLINQ.

1. Simple Example 1. Parallel.
Int [] nums = new [] {1, 2, 3, 4}; Parallel. for (0, nums. length, I => {Console. writeLine ("subscript: {0}, value: {1}", I, nums [I]);});
2. Parallel. ForEach
Var ltModel = new List <UserInfo> (); for (int I = 1; I <= 5; I ++) {var item = new UserInfo {No = I, name = "Name" + I}; ltModel. add (item);} Parallel. forEach (ltModel, item => {Console. writeLine ("No: {0}, Name: {1}", item. no, item. name );});
3. Parallel. Invoke
Var ltTask = new List <Action> (); for (int I = 1; I <= 5; I ++) {int j = I; ltTask. add () => {Console. writeLine ("START task" + j) ;}) ;} Parallel. invoke (ltTask. toArray ());
4. PLINQ
            var ltNum = new List<int>() {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};            var resultNum = (from n in ltNum.AsParallel() where n>=3 orderby n descending select n).ToArray();            foreach (var item in resultNum)            {                Console.WriteLine("Num: {0}", item);               }
Ii. Notes

If you have no requirements on the logical sequence of the set, you can use Parallel and PLINQ. Otherwise, they cannot be used.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.