Multi-threaded collection Security

Source: Internet
Author: User

Multi-threaded collection Security

If a set is used in multiple threads without any security processing, it is very easy to cause system crashes or various errors. In the latest project, a set is changed after socket communication. As a result, the system crashes directly without any errors.

After troubleshooting, it is found that the system exits after a set is executed within a certain period of time. Finally, it is found that a dictionary set is faulty. After thinking a little bit, we thought it was a thread security issue. This set has threads in several other places for loop reading.

The following is an example of my simulation, without any security processing:

1 class Program 2 {3 static MyCollection mycoll; 4 static void Main (string [] args) 5 {6 mycoll = new MyCollection (); 7 Thread readT = new Thread (new ThreadStart (ReadMethod); 8 readT. start (); 9 10 Thread addT = new Thread (new ThreadStart (AddMethod); 11 addT. start (); 12 Console. readLine (); 13} 14 public static void AddMethod () 15 {16 for (int I = 0; I <10; I ++) 17 {18 Thread. sleep (500); 19 mycoll. add ("a" + I, I); 20} 21} 22 public static void ReadMethod () 23 {24 while (true) 25 {26 Thread. sleep (100); 27 foreach (KeyValuePair <string, int> item in mycoll. myDic) 28 {29 Console. writeLine (item. key + "\ t" + item. value); 30 // other processing 31 threads. sleep (2000); 32} 33} 34} 35} 36 public class MyCollection37 {38 public Dictionary <string, int> myDic = new Dictionary <string, int> (); 39 40 public void Add (string key, int value) 41 {42 if (myDic. containsKey (key) 43 {44 myDic [key] + = 1; 45} 46 else47 {48 myDic. add (key, value); 49} 50} 51 52 public void Remove (string key) 53 {54 if (myDic. containsKey (key) 55 {56 myDic. remove (key); 57} 58} 59}

In the preceding example, a Dictionary object is created. When the program is running, the following error is output:

When the program runs, the above error is output, and only one row of results is output.

An error message is displayed in this test. The set has been modified. enumeration may not be performed.

Alas, this is a common problem. When you modify the set in foreach, the problem will certainly occur, because foreach is read-only, you cannot modify the set during the time period.

Here, we will think that if we use the for loop for reverse retrieval, this problem may be solved.

Unfortunately, the dictionary does not use the index number. The following table

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.