Control the number of enabled threads

Source: Internet
Author: User

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?

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.