Last active
February 9, 2016 11:08
-
-
Save denisivan0v/211def2dff82311820bb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Concurrent; | |
using System.Collections.Generic; | |
using System.Threading; | |
using System.Threading.Tasks; | |
namespace IteratorTests | |
{ | |
public static class Program | |
{ | |
public static void Main() | |
{ | |
try | |
{ | |
Test1(); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine(ex.ToString()); | |
} | |
Console.ReadKey(); | |
Console.WriteLine(); | |
try | |
{ | |
Test2(); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine(ex.ToString()); | |
} | |
Console.ReadKey(); | |
Console.WriteLine(); | |
try | |
{ | |
Test3(); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine(ex.ToString()); | |
} | |
Console.ReadKey(); | |
} | |
private static void Test1() | |
{ | |
var ten = new CollectionOfInts(new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }); | |
Run(ten); | |
} | |
private static void Test2() | |
{ | |
var ten = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; | |
Run(ten); | |
} | |
private static void Test3() | |
{ | |
var ten = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; | |
var counters = new ConcurrentDictionary<int, int>(); | |
Parallel.ForEach(ten, | |
item => | |
{ | |
counters.AddOrUpdate(item, key => 1, (key, value) => ++value); | |
if (ten.Contains(1)) | |
{ | |
Remove(ten, 1); | |
} | |
}); | |
foreach (var counter in counters) | |
{ | |
Print(counter); | |
} | |
} | |
private static void Run(ICollection<int> collection) | |
{ | |
var counters = new ConcurrentDictionary<int, int>(); | |
var tasks = new List<Task>(); | |
for (int i = 0; i < 16; i++) | |
{ | |
tasks.Add(Task.Run(() => | |
{ | |
foreach (var item in collection) | |
{ | |
counters.AddOrUpdate(item, key => 1, (key, value) => ++value); | |
Thread.Sleep(100); | |
} | |
})); | |
} | |
Remove(collection, 1); | |
Task.WaitAll(tasks.ToArray()); | |
foreach (var counter in counters) | |
{ | |
Print(counter); | |
} | |
} | |
private static void Remove(ICollection<int> list, int item) | |
{ | |
list.Remove(item); | |
} | |
private static void Print(KeyValuePair<int, int> pair) | |
{ | |
Console.WriteLine($"{pair.Key}: {pair.Value}"); | |
} | |
public class CollectionOfInts : ICollection<int> | |
{ | |
private readonly List<int> _inner; | |
public CollectionOfInts(int[] init) | |
{ | |
_inner = new List<int>(init); | |
} | |
public IEnumerator<int> GetEnumerator() | |
{ | |
for (var i = 0; i < _inner.Count; i++) | |
{ | |
yield return _inner[i]; | |
} | |
} | |
IEnumerator IEnumerable.GetEnumerator() | |
{ | |
return GetEnumerator(); | |
} | |
public void Add(int item) | |
{ | |
throw new NotImplementedException(); | |
} | |
public void Clear() | |
{ | |
throw new NotImplementedException(); | |
} | |
public bool Contains(int item) | |
{ | |
throw new NotImplementedException(); | |
} | |
public void CopyTo(int[] array, int arrayIndex) | |
{ | |
throw new NotImplementedException(); | |
} | |
public bool Remove(int item) | |
{ | |
return _inner.Remove(item); | |
} | |
public int Count => _inner.Count; | |
public bool IsReadOnly => false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment