. NET implements parallelism through Async/await

Source: Internet
Author: User

If parallelism can greatly improve performance, but in our use, it is not all parallel and wired line operation, so we need to do parallel operation in the business logic layer of the Protection exhibition:

The data access layer is unchanged or the same as before:

     Public classUserdal { PublicUser GetUser () {User User=NewUser (); User. Name="N1"; User. Address="A1"; returnuser; }         PublicList<user>getuserlist () {List<User> list =NewList<user>(); List. ADD (NewUser {Name ="N11", Address ="A11" }); List. ADD (NewUser {Name ="N12", Address ="A12" }); returnlist; }    }

The business logic layer needs to have async in the protection exhibition, as follows:

     Public classuserbll {userdal dal=NewUserdal ();  Public AsyncTask<user>Getuserasync () {return awaitTask.run (() = {returndal. GetUser ();        }); }         PublicUser GetUser () {returndal.        GetUser (); }         Public AsyncTask<list<user>>Getuserlistasync () {return awaitTask.run (() = {returndal. Getuserlist ();        }); }         PublicList<user>getuserlist () {returndal.        Getuserlist (); }    }

The last is called, as follows:

 USERBLL USERBLL =  USERBLL ();  public  async  task<  Actionresult> Index () { var             User = Userbll.getuserasync ();  var  listuser = Userbll.getuserlistasync            ();             int  t1 = Environment.tickcount;             await   Task.whenall (user, listuser); Viewbag.user  = User.            Result; Viewbag.listuser  = Listuser.result;  

Viewbag.user = await User;
Viewbag.listuser = await listuser;

            = Environment.tickcount- t1;             return View ();        }

Specific performance we can test, in the data access layer each time delay 500 milliseconds, with parallel and line line test, parallel about the time of about 500 milliseconds, and the line is about 1000 milliseconds

The above reference Dudu practical case: Implementing parallel http://www.cnblogs.com/dudu/p/async_await_parallel.html in existing code through ASYNC/AWAIT

. NET implements parallelism through Async/await

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.