Using system; <br/> using system. collections; <br/> using system. text; <br/> using system. threading; <br/> public class crunmain <br/>{< br/> Public static void main () <br/>{< br/> // syncthread1 (); <br/> // syncthread2 (); <br/> syncthread3 (); <br/>}< br/> Public static void syncthread1 () <br/>{< br/> ctest test = new ctest (1000); <br/> thread trd1 = new thread (New threadstart (test. add); <br/> thread trd2 = new thread (New threadstart (test. sub); <br/> trd1.start (); <br/> trd2.start (); <br/> while (true) <br/>{< br/> console. writeline ("=============================== main thread is running ================ ======= "); <br/> thread. sleep (100); <br/>}< br/> Public static void syncthread2 () <br/>{< br/> ctest2 test = new ctest2 (100); <br/> thread trd1 = new thread (New threadstart (test. add); <br/> thread trd2 = new thread (New threadstart (test. sub); <br/> trd1.start (); <br/> trd2.start (); <br/> while (true) <br/>{< br/> console. writeline ("=============================== main thread is running ================ ======= "); <br/> thread. sleep (100); <br/>}< br/> Public static void syncthread3 () <br/>{< br/> ctest3 test = new ctest3 (10); <br/> thread trd1 = new thread (New threadstart (test. add); <br/> thread trd2 = new thread (New threadstart (test. sub); <br/> trd1.start (); <br/> trd2.start (); <br/> while (true) <br/>{< br/> console. writeline ("=============================== main thread is running ================ ======= "); <br/> thread. sleep (1000); <br/>}< br/> public class ctest3 <br/>{< br/> private mutex m_objmutex; <br/> private int m_ncount = 0; <br/> Public ctest3 (INT nval) <br/>{< br/> m_objmutex = new mutex (); <br/> m_ncount = nval; <br/>}< br/> Public void add () <br/>{< br/> while (true) <br/>{< br/> m_objmutex.waitone (); <br/> m_ncount + = 1; <br/> console. writeline ("after add 1" + "/tncount =" + m_ncount); <br/> m_objmutex.releasemutex (); <br/> If (m_ncount> 0) <br/>{< br/> thread. sleep (1000); <br/>}< br/> Public void sub () <br/>{< br/> while (true) <br/>{< br/> m_objmutex.waitone (); <br/> m_ncount-= 1; <br/> console. writeline ("After sub 1" + "/tncount =" + m_ncount); <br/> m_objmutex.releasemutex (); <br/> If (m_ncount <0) <br/>{< br/> thread. sleep (1000 ); <br/>}</P> <p> public class ctest2 <br/>{< br/> private object m_objlock = new object (); <br/> private int m_ncount = 0; <br/> Public ctest2 (INT nval) <br/>{< br/> m_ncount = nval; <br/>}< br/> Public void add () <br/>{< br/> while (true) <br/>{< br/> monitor. enter (m_objlock); <br/> m_ncount + = 1; <br/> console. writeline ("after add 1" + "/tncount =" + m_ncount); <br/> monitor. exit (m_objlock); <br/> If (m_ncount> 0) <br/>{< br/> thread. sleep (100); <br/>}< br/> Public void sub () <br/>{< br/> while (true) <br/>{< br/> monitor. enter (m_objlock); <br/> m_ncount-= 1; <br/> console. writeline ("After sub 1" + "/tncount =" + m_ncount); <br/> monitor. exit (m_objlock); <br/> If (m_ncount <0) <br/>{< br/> thread. sleep (100 ); <br/>}< br/> public class ctest <br/>{< br/> private object objlock = new object (); <br/> private int m_ncount = 100; <br/> Public ctest (INT nval) <br/>{< br/> m_ncount = nval; <br/>}< br/> Public void add () <br/>{< br/> while (true) <br/>{< br/> lock (objlock) <br/>{< br/> m_ncount + = 1; <br/> console. writeline ("after add 1" + "/tncount =" + m_ncount); <br/>}< br/> If (this. m_ncount> 0) <br/>{< br/> thread. sleep (10); <br/>}< br/> Public void sub () <br/>{< br/> while (true) <br/>{< br/> lock (objlock) <br/>{< br/> m_ncount-= 1; <br/> console. writeline ("After sub 1" + "/tncount =" + m_ncount); <br/>}< br/> If (this. m_ncount <0) <br/>{< br/> thread. sleep (10); <br/>}< br/>}