Esempio n. 1
0
 private static void ThreadReaderWork(object obj)
 {
     for (int i = 1; i < 10; i++)
     {
         _limitedQueue.Deque();
     }
 }
Esempio n. 2
0
        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.");
        }
Esempio n. 3
0
 private static void reader(object obj)
 {
     for (int i = 0; i < 1000; i++)
     {
         Console.WriteLine("the item that was drown is " + _limitQueue.Deque());
     }
 }
Esempio n. 4
0
        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();
        }
Esempio n. 5
0
        public void GetQueueData()
        {
            string data = newLimitedQueue.Deque();

            Thread.Sleep(2000);
            Console.WriteLine("item {0} got out of the queue", data);
        }
Esempio n. 6
0
        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);
            }
        }
Esempio n. 7
0
        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()}");
                });
            }
        }
Esempio n. 8
0
        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} .");
                });
            }
        }
Esempio n. 9
0
        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();
        }
Esempio n. 10
0
        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);
            }
        }
Esempio n. 11
0
        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);
            }
        }
Esempio n. 12
0
        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();
        }