C # integrate data synchronization with multiple threads

Source: Internet
Author: User
This article mainly introduces C #Multithreading Set Data Synchronization. Collection classes are generally not thread-safe. Multiple readers can safely read the collection. However, any changes to the set will generate ambiguous results for all threads accessing the set. Any of the following methods can make the set class thread safe.

(1) If the synchronized method is used, the package is derived from this class and the package is used to exclusively access the set.

(2) If the class does not have the synchronized method, the synchronized method is derived from the class and implemented using the syncroot attribute.

(3) Lock the syncroot attribute when accessing this set

During this period of time, the company has made many multi-threaded things, so I wrote some experiences and gave a prompt to my friends who are interested in this part.

You can see the following code:

The following is a reference clip:
Class Program
{
Static void main (string [] ARGs)
{
Program Pg = new program ();
// Write thread
Thread T1 = new system. Threading. Thread (New threadstart (pg. t1fun ));
// Read thread
Thread t2 = new system. Threading. Thread (New threadstart (pg. t2fun ));
// Delete a thread
Thread T3 = new system. Threading. Thread (New threadstart (pg. t3fun ));
T1.start ();
T2.start ();
T3.start ();
}
Arraylist = new arraylist ();
Public void t1fun ()
{
While (true)
{
Arraylist. Add ("T1 -- write ");
System. Console. Out. writeline ("write ");
System. Threading. thread. Sleep (1000 );
}
}
Public void t2fun ()
{
While (true)
{
For (INT I = arraylist. Count-1; I> = 0; I --)
{
System. Console. Out. writeline ("T2 read:" + (string) arraylist [I]);
}
System. Threading. thread. Sleep (1000 );
}
}
Public void t3fun ()
{
While (true)
{
For (INT I = arraylist. Count-1; I> = 0; I --)
{
Arraylist. removeat (I );
System. Console. Out. writeline ("T3 Delete: T1" + I. tostring ());
}
System. Threading. thread. Sleep (1000 );
}
}
}

This test program is simple. You can see it at a Glance. You can run it to check whether the program crashes and discover exceptions.

Unprocessed exception: system. argumentoutofrangeexception:Index out of range. It must be a non-negative value and smaller than the set size.

This is caused by synchronization of shared set resources in multiple threads.

The modified code is as follows:

The following is a reference clip:
Class Program
{
Static void main (string [] ARGs)
{
Program Pg = new program ();
// Write thread
Thread T1 = new system. Threading. Thread (New threadstart (pg. t1fun ));
// Read thread
Thread t2 = new system. Threading. Thread (New threadstart (pg. t2fun ));
// Delete a thread
Thread T3 = new system. Threading. Thread (New threadstart (pg. t3fun ));
T1.start ();
T2.start ();
T3.start ();
}
Arraylist = new arraylist ();
Public void t1fun ()
{
While (true)
{
Lock (arraylist. syncroot)
{
Arraylist. Add ("T1 -- write ");
}
System. Console. Out. writeline ("write ");
System. Threading. thread. Sleep (1000 );
}
}
Public void t2fun ()
{
While (true)
{
Lock (arraylist. syncroot)
{
For (INT I = arraylist. Count-1; I> = 0; I --)
{
System. Console. Out. writeline ("T2 read:" + (string) arraylist [I]);
}
}
System. Threading. thread. Sleep (1000 );
}
}
Public void t3fun ()
{
While (true)
{
Lock (arraylist. syncroot)
{
For (INT I = arraylist. Count-1; I> = 0; I --)
{
Arraylist. removeat (I );
System. Console. Out. writeline ("T3 Delete: T1" + I. tostring ());
}
}
System. Threading. thread. Sleep (1000 );
}
}
}

 

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.