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 ();
}
}