static void Main(string[] args) { var random = new Random(); const int maxQueued = 3; const int taskNumber = 10; var limitedQueue = new LimitedQueue <int>(maxQueued); var tasks = new Task[taskNumber]; for (int i = 0; i < 10; i++) { int tastkId = i; tasks[i] = Task.Run(async() => { var randomNumber = random.Next(1, 1000); await Task.Delay(random.Next(1000, 3000)); Console.WriteLine($"Task {tastkId} begins and waits for Enque."); limitedQueue.Enque(randomNumber); Console.WriteLine($"Task {tastkId} added {randomNumber} To Queue."); await Task.Delay(random.Next(1000, 2000)); Console.WriteLine($"Task {tastkId} continues and tries to Deque."); Console.WriteLine($"Task {tastkId} removed {limitedQueue.Deque()} from queue."); }); } Task.WaitAll(tasks); Console.WriteLine(); Console.WriteLine("All tasks finished. No DeadLock detected."); }
static void Main(string[] args) { _limitQueue = new LimitedQueue <int>(50); ThreadPool.QueueUserWorkItem(writer); ThreadPool.QueueUserWorkItem(reader); Console.ReadLine(); }
static void Main(string[] args) { LimitedQueue <int> queue = new LimitedQueue <int>(1); Task.Run(() => { queue.Enqueue(1); Console.WriteLine(1); }); Task.Run(() => { queue.Enqueue(2); Console.WriteLine(2); }); Task.Run(() => { queue.Enqueue(3); Console.WriteLine(3); }); Task.Run(() => { Thread.Sleep(3000); queue.Dequeue(); Thread.Sleep(3000); queue.Dequeue(); }); Console.ReadLine(); }
public void RunQueues() { LimitedQueue <int> queue = new LimitedQueue <int>(10); for (int i = 0; i < 50; i++) { ThreadPool.QueueUserWorkItem((obj) => { Random random = new Random(); int newItem = random.Next(1, 50); if (newItem >= 25) { queue.Enque(newItem); Console.WriteLine("The operation is enque, queue: " + queue); } else { int dequeValue = queue.Deque(); if (dequeValue != 0) { Console.WriteLine("The operation is deque, queue: " + queue); } else { Console.WriteLine("The deque rerjected!"); } } Thread.Sleep(1000); }); } Console.Read(); }
static void Main(string[] args) { LimitedQueue <int> queue = new LimitedQueue <int>(2); WorkWithQueue <int> workWithQueue = new WorkWithQueue <int>(); workWithQueue.AddToQueue(queue, 5); workWithQueue.RemoveFromQueue(queue); }
static void Main(string[] args) { _limitedQueue = new LimitedQueue <int>(10); for (int i = 1; i < 21; ++i) { ThreadPool.QueueUserWorkItem(ThreadWriterWork, i++); ThreadPool.QueueUserWorkItem(ThreadReaderWork, i); } Console.ReadLine(); }
public void RemoveRandoly(LimitedQueue <int> myQ, CancellationTokenSource cancel, ref string rmvMsg) { int rmvNo = 0; Random r = new Random(); while (!cancel.IsCancellationRequested) { int element = myQ.Deque(); rmvNo++; rmvMsg = $"the last remove was of {element} from {myQ}\n it was remove no' {rmvNo}"; Thread.Sleep(r.Next() % 10000); } }
public void AddRandomly(LimitedQueue <int> myQ, CancellationTokenSource cancel, ref string addMsg) { int addingNo = 0; Random r = new Random(); while (!cancel.IsCancellationRequested) { int element = r.Next() % 10; myQ.Enque(element); addingNo++; addMsg = $"the last adding was of {element} to {myQ}\n it was adding no' {addingNo}"; Thread.Sleep(r.Next() % 1000); } }
public void RemoveFromQueue(LimitedQueue <T> queue) { Random rnd = new Random(); int count = rnd.Next(1, 5); while (count > 0) { Task.Factory.StartNew(() => { queue.Dequeue(); } ); count--; } }
public void AddToQueue(LimitedQueue <T> queue, T value) { Random rnd = new Random(); int count = rnd.Next(1, 5); while (count > 0) { Task.Factory.StartNew(() => { queue.Enqueue(value); } ); count--; } }
static void Main(string[] args) { LimitedQueue <int> queue = new LimitedQueue <int>(30); for (int i = 0; i < 40; i++) { var threadNumber = i; ThreadPool.QueueUserWorkItem(t => { Console.WriteLine($"Enque thread: {threadNumber}"); queue.Enque(threadNumber); Thread.Sleep(500); Console.WriteLine($"Deque thread: {queue.Deque()}"); }); } }
static void Main(string[] args) { LimitedQueue<int> queue = new LimitedQueue<int>(5); for (int i = 0; i < 20; i++) { int action = new Random().Next(0, 2); if (action == 0) { var trd = new Thread((j) => { try { Console.WriteLine($"{j}: Dequeued {queue.Dequeue()}"); Console.WriteLine($"{j}: {queue.Count()} "); } catch (InvalidOperationException ex) { Console.WriteLine($"{j}: queue is empty"); } } ); trd.Name = i.ToString(); trd.Start(i); } else { int value = new Random().Next(0, 200); var trd = new Thread((j) => { Console.WriteLine($"{j}: Try to enqueue {value}"); queue.Enque(value); Console.WriteLine($"{j}: Enqueued {value}"); Console.WriteLine($"{j}: {queue.Count()} "); }); trd.Name = i.ToString(); trd.Start(i); } } Console.ReadLine(); }
static void Main(string[] args) { LimitedQueue <int> queue = new LimitedQueue <int>(2); for (int i = 0; i < 10; i++) { var tI = i; ThreadPool.QueueUserWorkItem(s => { Console.WriteLine($"Enque: {tI}"); queue.Enque(tI); Thread.Sleep(1000); Console.WriteLine($"Deque: {queue.Deque()}"); }); } Console.ReadKey(); }
static void Main(string[] args) { LimitedQueue <int> myQueue = new LimitedQueue <int>(100); { for (int i = 0; i < 1000; i++) { if (i % 3 == 0) { ThreadPool.QueueUserWorkItem((o) => myQueue.Dequeue(), null); } else { ThreadPool.QueueUserWorkItem((o) => myQueue.Enqueue(new Random().Next()), null); } } Console.ReadLine(); } }
public void TestingCode() { LimitedQueue <int> queue = new LimitedQueue <int>(10); for (int i = 1; i <= 20; i++) { var number = i; Task.Run(async() => { queue.Enque(number); Console.WriteLine($"Added to queue: {number} ."); await Task.Delay(2000); var dequeNumber = queue.Deque(); Console.WriteLine($"Removed from queue: {dequeNumber} ."); }); } }
static void Main(string[] args) { var canecellation = new CancellationTokenSource(); var cToken = canecellation.Token; string addMsg = "", rmvMsg = ""; var mychecker = new LimitedQueueChecker(); var myLimitedQ = new LimitedQueue <int>(5); Task.Run(() => mychecker.AddRandomly(myLimitedQ, canecellation, ref addMsg)); Task.Run(() => mychecker.RemoveRandoly(myLimitedQ, canecellation, ref rmvMsg)); Console.WriteLine("\n We start runnig the Check App. press any key to see What happend\n # press esc to exit"); while (Console.ReadKey(true).Key != ConsoleKey.Escape) { Console.WriteLine(addMsg); Console.WriteLine(rmvMsg); Console.WriteLine($"the {myLimitedQ} count is {myLimitedQ.Count}"); } canecellation.Cancel(); Console.WriteLine(" goodbuy :)"); }
static void Main(string[] args) { LimitedQueue <int> limitedQueue = new LimitedQueue <int>(10); Random random = new Random(); int currentRandNum; for (int i = 0; i < 10; i++) { currentRandNum = random.Next(1, 50); Console.WriteLine("Adding item"); ThreadPool.QueueUserWorkItem(q => limitedQueue.Enque(currentRandNum)); Thread.Sleep(500); } for (int i = 0; i < 10; i++) { Console.WriteLine("Removing item"); ThreadPool.QueueUserWorkItem(q => limitedQueue.Deque()); Thread.Sleep(500); } }
public static void Main(string[] args) { LimitedQueue <int> testingQueue = new LimitedQueue <int>(5); for (int i = 0; i <= 21; i++) { Console.WriteLine($"Queue size is {testingQueue.Count}"); Random rand = new Random(); int currRand = rand.Next(1, 101); if (currRand % 3 != 0) { Console.WriteLine("Start adding process"); ThreadPool.QueueUserWorkItem(q => testingQueue.Enque(i)); } else { Console.WriteLine("Start removing process"); ThreadPool.QueueUserWorkItem(q => testingQueue.Deque()); } Thread.Sleep(1000); } }
static void Main(string[] args) { int maxSize = 10; LimitedQueue <int> queue = new LimitedQueue <int>(maxSize); for (int i = 0; i < 10; i++) { ThreadPool.QueueUserWorkItem((_) => { Console.WriteLine($"thread:{Thread.GetDomainID()}"); Random r = new Random(); for (int j = 0; j < 100; j++) { if (r.Next(4) > 1) { queue.Enque(r.Next(10)); Console.WriteLine("Enque"); //write operation } else { queue.Deque(); Console.WriteLine("Deque"); //write operation } Thread.Sleep(r.Next(200)); Console.WriteLine(queue.ToString()); //read operation if (queue.Count > maxSize) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine($"ERROR!"); } Console.WriteLine($"count:{queue.Count}"); ; } }); } Console.Read(); }