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 ); } } } |