Thread Pool read List & lt; T & gt; instance, thread list

Source: Internet
Author: User

Thread Pool read List <T> instance, thread list

Private static readonly Object ThisLock = new object (); private static readonly AutoResetEvent AutoReset = new AutoResetEvent (false); private static int _ selectindex; private const int Threadcount = 10; private int _ finishcount; List <T> _ datamodellist; public void LoadPage () {Grid ();} private void Grid (){
_ List. clear (); _ finishcount = 0; _ selectindex = 0; _ datamodellist = GetAll (); for (var I = 0; I <Threadcount; I ++) {var thread = new Thread (DoTask); thread. start (1);} AutoReset. waitOne (); // wait for the thread pool to be completed // do something} public void DoTask (Object index) {while (_ selectindex <_ datamodellist. count) {lock (ThisLock) // after locking, only one thread enters until the code in the block is executed {if (_ selectindex> = _ datamodellist. count) continue; daimamodel = _ datamodellist [_ selectindex]; _ selectindex ++;} // do something _ finishcount ++; if (_ finishcount = _ datamodellist. count) {AutoReset. set (); // terminate after the last thread is completed
}
}
}

 

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.