Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. text; <br/> using system. threading; </P> <p> namespace multithreading <br/>{< br/> simple class statement atomicity <br/>{< br/> Public static void main () <br/> {<br/> // The interlocked class provides some simple synchronization operations, such as ++. <Br/> // The increment () method increments a variable and stores the result in an atomic operation. <br/> person per = new person (); </P> <p> for (INT I = 0; I <30; I ++) <br/> {<br/> New thread (doincermentthread ). start (PER); <br/>}</P> <p> console. writeline (Per. age); <br/> thread. sleep (1000); <br/> console. writeline (Per. age); <br/> thread. sleep (1000); <br/> console. writeline (Per. age); <br/> thread. sleep (1000); <br/> console. writeline (per. Age); <br/> thread. sleep (1000); <br/> console. writeline (Per. age); <br/> thread. sleep (1000); <br/> console. writeline (Per. age); </P> <p> console. readline (); </P> <p >}</P> <p> Public static void doincermentthread (Object per) <br/>{< br/> person P = (person) per; </P> <p> for (INT I = 0; I <1000000; I ++) <br/>{< br/> P. incermentage (); <br/>}</P> <p> class person <br/>{< br/> private Int _ age; </P> <p> Public int age <br/>{< br/> get {return _ age ;} <br/>}</P> <p> Public int incermentage () <br/>{< br/> return interlocked. increment (ref this. _ age); <br/> // If ++ is used, the thread may be interrupted. To ensure synchronization, you can use lock .. More efficient: Use the increment method <br/> // return ++ this. _ age; <br/>}< br/>