A simple example of C # multi-thread synchronization

Source: Internet
Author: User

In development, we often encounter thread examples. If a background operation is time-consuming, we can start a thread to execute that time-consuming operation, and the program continues to execute. In some cases, synchronization and collaboration among multiple threads may occur. The following example shows how to work collaboratively between two threads.

The idea of this program is to do one thing together (delete elements from an ArrayList). If the execution is complete, both threads stop running.

The Code is as follows:


Using System;
Using System. Collections;
Using System. Collections. Generic;
Using System. Threading;

/// <Summary>
/// Example of a thread is often encountered during development. If a background operation is time-consuming, we can start a thread to execute the time-consuming operation, and the program continues to execute it. In some cases, synchronization and collaboration among multiple threads may occur. The following example shows how to work collaboratively between two threads.
///
/// The idea of this program is to do one thing together (delete elements from an ArrayList). If the execution is complete, both threads stop.
/// By Zhou Gong
/// Time:
// Original address: http://blog.csdn.net/zhoufoxcn
/// </Summary>
Public class ThreadDemo
{
Private Thread threadOne;
Private Thread threadTwo;
Private ArrayList stringList;
Private event EventHandler OnNumberClear; // event triggered by data deletion completion
Public static void Main ()
{
ThreadDemo demo = new ThreadDemo (1000 );
Demo. Action ();
}
Public ThreadDemo (int number)
{
Random random = new Random (1000000 );
StringList = new ArrayList (number );
For (int I = 0; I <number; I ++)
{
StringList. Add (random. Next (). ToString ());
}
ThreadOne = new Thread (new ThreadStart (Run); // two threads work together to do one thing
ThreadTwo = new Thread (new ThreadStart (Run); // The two threads work together to do one thing.
ThreadOne. Name = "thread 1 ";
ThreadTwo. Name = "thread 2 ";
OnNumberClear + = new EventHandler (ThreadDemo_OnNumberClear );

}
/// <Summary>
/// Start work
/// </Summary>
Public void Action ()
{
ThreadOne. Start ();
ThreadTwo. Start ();
}
/// <Summary>
/// Work done together
/// </Summary>
Private void Run ()
{
String stringValue = null;
While (true)
{
Monitor. Enter (this); // locked to maintain synchronization
StringValue = (string) stringList [0];
Console. WriteLine (Thread. CurrentThread. Name + "deleted" + stringValue );
StringList. RemoveAt (0); // Delete the element in ArrayList
If (stringList. Count = 0)
{
OnNumberClear (this, new EventArgs (); // triggers a completion event
}
Monitor. Exit (this); // unlock
Thread. Sleep (5 );
}
}

// After the execution is complete, stop all threads
Void ThreadDemo_OnNumberClear (object sender, EventArgs e)
{
Console. WriteLine ("the execution is finished and all threads are stopped. ");
ThreadTwo. Abort ();
ThreadOne. Abort ();

}
}
Note: More than this method is required for thread synchronization. The event is used here, And the thread is aborted in the event handler.

 


OnNumberClear + = new EventHandler (ThreadDemo_OnNumberClear );
OnNumberClear is an event
EventHandler is the delegate
ThreadDemo_OnNumberClear is the method name.
The code above indicates that when the OnNumberClear event is triggered, it will be processed by the ThreadDemo_OnNumberClear method.

Related Article

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.