예제 #1
0
        public void ParallelTest()
        {
            var _NumberOfItems = 100000;
            var _ConcurrentBag = new ConcurrentBag<UniqueTimestamps>();

            var _Task1 = Task.Factory.StartNew(() =>
            {
                Parallel.For(0, _NumberOfItems, i =>
                {
                    var _UniqueTS = new UniqueTimestamps();
                    _UniqueTS.ThreadId  = Thread.CurrentThread.ManagedThreadId;
                    _UniqueTS.Timestamp = UniqueTimestamp.Ticks;
                    _ConcurrentBag.Add(_UniqueTS);
                });
            });

            var _Task2 = Task.Factory.StartNew(() =>
            {
                Parallel.For(0, _NumberOfItems, j =>
                {
                    var _UniqueTS = new UniqueTimestamps();
                    _UniqueTS.ThreadId  = Thread.CurrentThread.ManagedThreadId;
                    _UniqueTS.Timestamp = UniqueTimestamp.Ticks;
                    _ConcurrentBag.Add(_UniqueTS);
                });
            });

            Task.WaitAll(_Task1, _Task2);

            var _NumberOfThreads    = (from item in _ConcurrentBag select item.ThreadId ).Distinct().Count();
            var _NumberOfTimestamps = (from item in _ConcurrentBag select item.Timestamp).Distinct().Count();

            Assert.AreEqual(_NumberOfThreads, Environment.ProcessorCount);
            Assert.AreEqual(_NumberOfTimestamps, 2 * _NumberOfItems);
        }
예제 #2
0
        public void ParallelTest()
        {
            var _NumberOfItems = 100000;
            var _ConcurrentBag = new ConcurrentBag <UniqueTimestamps>();

            var _Task1 = Task.Factory.StartNew(() =>
            {
                Parallel.For(0, _NumberOfItems, i =>
                {
                    var _UniqueTS       = new UniqueTimestamps();
                    _UniqueTS.ThreadId  = Thread.CurrentThread.ManagedThreadId;
                    _UniqueTS.Timestamp = UniqueTimestamp.Ticks;
                    _ConcurrentBag.Add(_UniqueTS);
                });
            });

            var _Task2 = Task.Factory.StartNew(() =>
            {
                Parallel.For(0, _NumberOfItems, j =>
                {
                    var _UniqueTS       = new UniqueTimestamps();
                    _UniqueTS.ThreadId  = Thread.CurrentThread.ManagedThreadId;
                    _UniqueTS.Timestamp = UniqueTimestamp.Ticks;
                    _ConcurrentBag.Add(_UniqueTS);
                });
            });

            Task.WaitAll(_Task1, _Task2);

            var _NumberOfThreads    = (from item in _ConcurrentBag select item.ThreadId).Distinct().Count();
            var _NumberOfTimestamps = (from item in _ConcurrentBag select item.Timestamp).Distinct().Count();

            Assert.AreEqual(_NumberOfThreads, Environment.ProcessorCount);
            Assert.AreEqual(_NumberOfTimestamps, 2 * _NumberOfItems);
        }