1 usingSystem;2 usingSystem.Collections;3 usingSystem.Collections.Generic;4 usingSystem.Collections.Concurrent;5 usingSystem.Linq;6 usingSystem.Threading;7 usingSystem.Threading.Tasks;8 9 Ten //Sample implementation of Iproducerconsumercollection (T) One //--In this case, a thread-safe stack. A Public classSafestack<t>: iproducerconsumercollection<t> -{ - //used for enforcing thread-safety the Private ObjectM_lockobject =New Object(); - - //We 'll use the a regular old Stack for our core operations - PrivateStack<t> M_sequentialstack =NULL; + - // + //Constructors A // at PublicSafestack () -{ -M_sequentialstack =NewStack<t> (); -} - - PublicSafestack (ienumerable<t> Collection) in{ -M_sequentialstack =NewStack<t> (collection); to} + - // the //Safe Push/pop Support * // $ Public voidPush (T Item)Panax Notoginseng{ - Lock(m_lockobject) M_sequentialstack.push (item); the} + A Public BOOLTrypop ( outT Item) the{ + BOOLRval =true; - Lock(M_lockobject) ${ $ if(M_sequentialstack.count = = 0) {item =default(T); Rval =false; } - Elseitem = M_sequentialstack.pop (); -} the returnRval; -}Wuyi the // - //Iproducerconsumercollection (T) support Wu // - Public BOOLTryTake ( outT Item) About{ $ returnTrypop ( outItem); -} - - Public BOOLTryAdd (T Item) A{ +Push (item); the return true;//Push doesn ' t fail -} $ the PublicT[] ToArray () the{ thet[] Rval =NULL; the Lock(m_lockobject) Rval = M_sequentialstack.toarray (); - returnRval; in} the the Public voidCopyTo (t[] array,intIndex About{ the Lock(m_lockobject) M_sequentialstack.copyto (array, index); the} the + - the //Bayi //support for IEnumerable (T) the // the PublicIenumerator<t> GetEnumerator () -{ - //The performance here is unfortunate for large stacks, the //But thread-safety is effectively implemented. theStack<t> stackcopy =NULL; the Lock(m_lockobject) Stackcopy =NewStack<t> (M_sequentialstack); the returnStackcopy.getenumerator (); -} the the the //94 // support for IEnumerable the // theIEnumerator Ienumerable.getenumerator () the{98 return((ienumerable<t>) This). GetEnumerator (); About} - 101 // 102 // support for ICollection103 //104 Public BOOLIsSynchronized the{106 Get{return true; }107}108 109 Public ObjectSyncRoot the{111 Get{returnM_lockobject; } the}113 the Public intCount the{ the Get{returnM_sequentialstack.count; }117}118 119 Public voidCopyTo (array array,intIndex -{121 Lock(M_lockobject) ((ICollection) m_sequentialstack). CopyTo (array, index);122}123}124 the Public classProgram126{127 Static voidMain () -{129Testsafestack (); the 131 //Keep the console window open in debug mode. theConsole.WriteLine ("Press any key to exit.");133Console.readkey ();134}135 136 //Test Our implementation of Iproducerconsumercollection (T)137 //demonstrates:138 //IPCC (T). TryAdd ()139 //IPCC (T). TryTake () $ //IPCC (T). CopyTo ()141 Static voidTestsafestack ()142{143safestack<int> stack =Newsafestack<int> ();144iproducerconsumercollection<int> IPCC = (iproducerconsumercollection<int>) stack;145 146 //Test Push ()/tryadd ()147Stack. Push (10); Console.WriteLine ("pushed ten");148IPCC. TryAdd (20); Console.WriteLine ("IPCC. tryadded");149Stack. Push (15); Console.WriteLine ("pushed"); Max 151 int[] Testarray =New int[3]; the 153 //Try CopyTo () within Boundaries154 Try155{156IPCC. CopyTo (Testarray, 0);157Console.WriteLine ("CopyTo () within boundaries worked, as expected");158}159 Catch(Exception e) the{161Console.WriteLine ("CopyTo () Within boundaries unexpectedly threw an exception: {0}", e.message);162}163 164 //Try CopyTo () that overflows165 Try166{167IPCC. CopyTo (Testarray, 1);168Console.WriteLine ("CopyTo () with index overflow worked, and it should not has");169} the Catch(Exception e)171{172Console.WriteLine ("CopyTo () with index overflow threw an exception, as expected: {0}", e.message);173}174 175 //Test enumeration176Console.Write ("enumeration (should be three items):");177 foreach(intIteminchStack) Console.Write ("{0}", item);178Console.WriteLine ("");179 the //Test Trypop ()181 intpopped = 0;182 if(Stack. Trypop ( outpopped))183{184Console.WriteLine ("successfully popped {0}", popped);185}186 ElseConsole.WriteLine ("FAILED to pop!!");187 188 //Test Count189Console.WriteLine ("stack count is {0}, should be 2", Stack. Count); the 191 //Test TryTake ()192 if(IPCC. TryTake ( outpopped))193{194Console.WriteLine ("successfully ipcc-trytaked {0}", popped);195}196 ElseConsole.WriteLine ("FAILED to the IPCC. trytake!!");197}198}199
. NET parallel Computing and Concurrency 11: concurrent Interface Iproducerconsumercollection