C # multithreading I

Source: Internet
Author: User
C # multithreading I

Using system;
Using system. Threading;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;

Namespace a0300_thread.sample
{

/// <Summary>
/// The simplest example of a thread
///
/// The thread execution method is defined as a static method.
/// </Summary>
Class staticthreadsample
{

/// <Summary>
/// Simple thread execution method.
///
/// This method is static
/// </Summary>
Public static void threadfunc ()
{
// Indicates that the thread stops running.
Boolean done = false;

// Counter
Int count = 0;

While (! Done)
{
// Sleep for 1 second.
Thread. Sleep (1000 );

// Counter increments
Count ++;

// Output.
Console. WriteLine ("[Static] execution times: {0}", count );
}
}

/// <Summary>
/// Code of the startup thread.
/// </Summary>
Public static void StartThread ()
{
ThreadStart ts = new ThreadStart (ThreadFunc );
Thread t = new Thread (ts );

// Start.
T. Start ();
}

}

}

 

 

Using system;
Using system. Threading;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;

Namespace a0300_thread.sample
{

/// <Summary>
/// The simplest example of a thread
///
/// The thread execution method is defined as a normal method.
/// </Summary>
Class threadsample
{

/// <Summary>
/// Simple thread execution method.
///
/// This method is not static
/// </Summary>
Public void threadfunc ()
{
// Indicates that the thread stops running.
Boolean done = false;

// Counter
Int COUNT = 0;

While (! Done)
{
// Sleep for 2 seconds.
Thread. Sleep (2000 );

// Counter increments
Count ++;

// Output.
Console. writeline ("[Normal] execution times: {0}", count );
}
}

/// <Summary>
/// Code of the startup thread.
///
///
/// Note: the difference between a static method and a common method in multithreading is that a common method needs to create an instance of the class.
/// </Summary>
Public static void StartThread ()
{

ThreadSample sample = new ThreadSample ();
ThreadStart ts = new ThreadStart (sample. ThreadFunc );

Thread t = new Thread (ts );

// Start.
T. Start ();
}

}

}

 

 

Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;

Using A0300_Thread.Sample;

Namespace A0300_Thread
{
Class Program
{
Static void Main (string [] args)
{
// Static thread method.
Staticthreadsample. startthread ();

// Common thread method.
Threadsample. startthread ();

Console. writeline ("press Ctrl + C to end the operation! ");
}
}
}

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.