Several Opinions on multithreading and Asynchronization

Source: Internet
Author: User
Several Opinions on multithreading and Asynchronization

Zengjun.cnblogs.com

This is a younger brother's virgin article. I used to read the article only in the garden.
Beginning of Zhengfang

There is a project in the work. Due to frequent file write operations
I used to write files synchronously. Someone suggested that I write files in multiple threads.
The multi-thread file writing task is assigned to another colleague,
Because another colleague is not familiar with multithreading, I gave him the following test:

// Multi‑thread operation synchronous file Writing Method
Public threadoperate ()
{
Thread t = new thread (New threadstart (threadproc ));
T. Start ();
}

// Synchronous file Writing Method
Public static void threadproc ()
{

}

Some data http://dev.yesky.com/135/3030135.shtml (multi-core era of concurrent programming exploration)
The following sentence is summarized:
Thread: the operating system needs to invest CPU resources for running and scheduling.
Asynchronous: operations that do not consume CPU time, such as I/O operations include not only direct file and network read/write, but also database operations, Web Service, httprequest, and. net remoting and other cross-process calls.
Currently, it cannot be determined whether the above sentence is correct. Please advise.

According to the above sentence, my idea has changed. I think it is more appropriate to write files asynchronously than multithreading.

// Write files Asynchronously
Public void writefile ()
{
Filestream FS = new filestream (@ "C: \ test.txt", filemode. Create, fileaccess. Write, fileshare. Write );
Byte [] buffer = new byte [100];
For (INT I = 0; I <buffer. length; I ++)
{
Buffer [I] = 2;
}
FS. beginwrite (buffer, 0, buffer. length, filecallback, FS );
}
 
Static void filecallback (iasyncresult asyncresult)
{
Filestream FS = asyncresult. asyncstate as filestream;
If (FS! = NULL)
{
FS. endwrite (asyncresult );
FS. Close ();

}
}

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.