Created
March 8, 2017 14:35
-
-
Save nicholasyin/a9b1d933fd2f610645f3f3beefe2c653 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.Generic; | |
using System.Data.Entity; | |
using System.Linq; | |
using System.Text; | |
using System.Threading; | |
using System.Threading.Tasks; | |
namespace ConsoleApplication1 | |
{ | |
class Program | |
{ | |
private static Random random = new Random(); | |
private static object locker = new object(); | |
private static List<string> items = new List<string>(); | |
static void Main(string[] args) | |
{ | |
for (int i = 0; i < 1000; i++) | |
{ | |
var thread = new Thread(new ThreadStart(Try)); | |
thread.Start(); | |
} | |
Thread.Sleep(1000); | |
Console.WriteLine($"Total: {items.Count}, Distinct: {items.Distinct().Count()}"); | |
} | |
static void Try() | |
{ | |
lock (locker) | |
{ | |
string value = string.Empty; | |
do | |
{ | |
value = random.Next(0, 99999).ToString(); | |
} | |
while (items.Contains(value)); | |
items.Add(value); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment