Simple thread processing

Source: Internet
Author: User

The followingCodeThe example shows the simple thread processing function.

Using system;
Using system. Threading;

// Simple threading scenario: Start a static method running
// On a second thread.
Public class threadexample {
// The threadproc method is called when the thread starts.
// It loops ten times, writing to the console and yielding
// The rest of its time slice each time, and then ends.
Public static void threadproc (){
For (INT I = 0; I <10; I ++ ){
Console. writeline ("threadproc: {0}", I );
// Yield the rest of the time slice.
Thread. Sleep (0 );
}
}

Public static void main (){
Console. writeline ("main thread: Start a second thread .");
// The constructor for the Thread class requires a threadstart
// Delegate that represents the method to be executed on
// Thread. C # simplifies the creation of this delegate.
Thread t = new thread (New threadstart (threadproc ));
// Start threadproc. On a uniprocessor, the thread does not get
// Any processor time until the main thread yields. uncomment
// The thread. Sleep that follows T. Start () to see the difference.
T. Start ();
// Thread. Sleep (0 );

For (INT I = 0; I <4; I ++ ){
Console. writeline ("main thread: Do some work .");
Thread. Sleep (0 );
}

console. writeline ("main thread: Call join (), to wait until threadproc ends. ");
T. join ();
console. writeline ("main thread: threadproc. join has returned. press enter to end program. ");
console. readline ();
}< BR >}

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.