1. How to control the number of threads?
2. How to prevent multiple threads from downloading the same webpage?
3. How to Determine the end of a thread?
4. How to control the end of a thread?
Below are some solutions to these problems:
1. We can implement the number of threads through the for loop, just like the hitting program for beginners.
For example, if you know that you have specified N (an int variable) threads, you can use the following method to open five threads.
Thread [] downloadthread;
// The famous download thread. This is the advantage of C #, that is, the length of the array does not need to be specified during initialization. It can be specified only during use.
This should be a class level, which makes it possible for other method controls.
Threadstart startdownload = new threadstart (download );
// Thread start setting: that is, each thread executes download ()
Downloadthread = new thread [N]; // apply for resources for the thread and determine the total number of threads
For (INT I = 0; I <n; I ++) // enables the specified number of threads
{
Downloadthread [I] = new thread (startdownload); // specifies the thread start setting.
Downloadthread [I]. Start (); // enable threads one by one
}
Okay. Is it easy to implement control on the number of threads enabled?