Use the. NET thread pool to improve application performance.

Source: Internet
Author: User
Tags net thread


Usually in the application of big data computingProgramCPU processing wait for most of the time. in a single-threaded application, these queries will cause the window message processing capability to be blocked, resulting in a long computing time and low CPU utilization.
Recently, we have been researching methods to improve processing performance.
Start to use, multi-thread, asynchronous methods to implement,
The following statement is used for data integration in segments (such as integrating intermediate data by day or by month ).
During the execution time of a single thread, the CPU utilization is generally between 1 and 10% (the server HP 580 4 CPU Xeon 3.0g 16g memory, and the disk cabinet is added), so the integration time is too long.
If segmented integration is used, the CPU utilization is greatly increased from 40 to 80%.

Fileinfo [] files;
Private   Void Button#click ( Object Sender, system. eventargs E)
{
If (Folderbrowserdialog1.showdialog () = Dialogresult. OK)
{
System. Io. directoryinfo di =   New Directoryinfo (folderbrowserdialog1.selectedpath );
Files = Di. getfiles ();


Threadstart TS =   New Threadstart (start );
Thread thread =   New Thread (TS );
Thread. Start ();


}
}
Public Delegate   String Mymethoddelegate ( Object [] ARGs );
Static   Int T =   0 ;
Void Start ()
{
Foreach (Fileinfo File In Files)
{
System. Io. streamreader SR =   New Streamreader (file. fullname, system. Text. encoding. Default );
String Txt = Sr. readtoend ();
Sr. Close ();
Mymethoddelegate ddd =   New Mymethoddelegate (work2 );
Ddd. begininvoke ( New   Object [] {Txt_connectstring.text, txt, file. fullname} , New Asynccallback (endworkback ), Null );

Write ( String . Format ( " T: {0} | {1} {1} \ r \ n " , Datetime. Now, file. fullname, t ));

// _ Task. starttask (New taskdelegate (_ task. work2), new object [] {txt_connectstring.text, TXT });

}

}
Public   String Work2 ( Object [] ARGs)
{
Using (Sqlconnection Conn =   New Sqlconnection (ARGs [ 0 ]. Tostring ()))
{
Using (Sqlcommand comm =   New Sqlcommand (ARGs [ 1 ]. Tostring (), Conn ))
{< br> comm. commandtimeout = 60000 ;
Conn. open ();
comm. executenonquery ();
}
Conn. Close ();
}
Return ARGs [ 2 ]. Tostring ();


}
Int I = 0 ;
Protected   Void Endworkback (iasyncresult AR)
{
Mymethoddelegate ddd = (Mymethoddelegate) Ar. asyncstate;
String Result = Ddd. endinvoke (AR );
I ++ ;
Write ( String . Format ( " T: {3 }:{ 2 }:{ 0} {1} \ r \ n " , Result, datetime. Now, I, T ));
}  
Public   Void Write ( String MSG)
{< br> system. io. streamwriter SW = New streamwriter ( " C: \ install. log " , true , system. text. encoding. default);
SW. writeline (MSG);
SW. close ();

}

However, the above program also has a problem, that is, the asynchronous thread data is not properly controlled. If too many threads are executed at the same time, the CPU time slice allocation is unreasonable and the execution speed is slow.
Later, I always wanted to use the thread pool to handle threads reasonably.
. Net built-in thread pool is relatively simple to use.
You can use the system. Threading. threadtool class,
Define the callback Method
Waitcallback async = new waitcallback (fexeute );
Use threadpool. queueuserworkitem (async, service); to join the queue
The callback method executes the operation.

Public Void Fexeute ( Object Param)
{
// Myresetevent. waitone ();
Using (Tg_evalutebasedatasset Service = (Tg_evalutebasedatasset) PARAM)
{< br> service. doresult ();
writefile (path, service);
ontgreport ( New reportevent (service. te_pass_range, service. te_year, service. te_month, int . parse (service. te_ I _e_flag), int . parse (service. te_entry_type);
}
Interlocked. decrement ( Ref Number );
// Number --;

}

interlocked. decrement: Call an atomic operation to perform the subtraction operation.
interlocked. increment (ref number); is to call atomic operations, add
atomic operations in multiple threads will not conflict.
several advantages of the thread pool
1. Multi-threaded execution,
2. Reasonably arrange the execution thread based on the number of CPUs and performance. (in the actual operation process, check the number of active processes in the SQL Server Enterprise Manager, the number of threads currently being executed.)
3. Asynchronous Operation.
related resources:
http://www.ddvip.net/program/c-/index2/15.htm
http://www.codeproject.com/csharp/threadtests.asp
http://www.microsoft.com/china/MSDN/library/netFramework/netframework/NECLRT.mspx

source code download:
http://files.cnblogs.com/edobnet/Report.rar

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.