In C #, why can I create threads with the same name? How can I differentiate threads with the same name? For example, set two threads with the same name to have different priorities?

Source: Internet
Author: User
Tags variable scope
Supplement: for example: (IntI =0I <10I ++)
{
Thread t =NewThread () T. Start ()
}

 

 
This is not a thread with the same name
Thread t = new thread ();
In each loop, the T variable in the previous loop has expired. This is a problem with the variable scope. Please refer to this document.
C # allows the same name, but the two variables have different scopes, such:
 Class {  Private   String Name; //  Name1, class member name (global variable)     Public A ( String Name) // Name2, method parameters  {  This . Name = Name; name1 name2}  Public   Void  B (){  String Name = "  Love  "  ;  This . Name = Name;  // Class member local variables  }} 
 
You can manage threads with the same name, such as placing an array:

Thread threads [] = new thread [10];
For (INT I = 0 I <10 I ++ ){
Threads [I] = new thread ()
Threads [I]. Start ();
}
 
A thread is the smallest executable unit. In your example, the thread executes every loop. So there is nothing that cannot be executed with the same name. You should first understand what a thread is.

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.