Multi-threaded operations include operations with or without Parameters

Source: Internet
Author: User

String [] urlcp = getpage (curl );
Thread [] mythread = new thread [urlcp. Length];
For (INT I = 0; I <urlcp. length; I ++)
{
// Getcontent (curl );
Getcontent (urlcp [I]);
Object [] urcp1 = (object []) urlcp;
Mythread [I] = new thread (New parameterizedthreadstart (getcontent1 ));
Mythread [I]. Start (urcp1); // pass the Parameter

}

 

Parameterizedthreadstart with object parameters: Public void start ()
{
For (INT Index = 0; index <10; index ++)
{

# Region -- prepare parameters --
List <int> List = new list <int> ();
For (INT Param = index; Param <index + 10; Param ++)
{
List. Add (PARAM );
}
# Endregion

Thread thread = new thread (New parameterizedthreadstart (initthreadfunc ));
Thread. Start (list );
}
Loghelper. writeline ("over ");

}

/// <Summary>
/// Executed Function
/// </Summary>
/// <Param name = "list"> </param>
Private void initthreadfunc (Object List)
{
If (list is list <int>)
{
List <int> LL = List as list <int>;
Int result = 0;
Foreach (int n in LL)
{
Result + = N;
}
Loghelper. writeline (result );
}
}

Second:


Initthreadfunc without Parameters

Public void start ()
{

For (INT Index = 0; index <10; index ++)
{

Thread thread = new thread (New threadstart (initthreadfunc ));
Thread. Start ();
}
Loghelper. writeline ("over ");

}

Private void initthreadfunc (){}

 

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.