Exemple #1
0
        public void _0006()
        {
            var persons = new MockCollection();
            var th1     = new Thread(new ThreadStart(() => {
                for (var i = 1; i < 100; i++)
                {
                    persons.Add(new PersonItem("Homer", "Simpson"));
                    lock ((persons as ICollection).SyncRoot){
                        Thread.Sleep(200);
                    }
                }
            }));
            var th2 = new Thread(new ThreadStart(() => {
                for (var i = 1; i < 100; i++)
                {
                    lock ((persons as ICollection).SyncRoot){
                        persons.Add(new PersonItem("Bart", "Simpson"));
                        Thread.Sleep(50);
                    }
                }
            }));

            th1.Start(); th2.Start();
            th1.Join(); th2.Join();
            //Homer
            //Bart
            //Homer
            //Bart
            //Homer
            //Bart
        }