Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Threading;
- using System.Threading.Tasks;
- namespace x
- {
- public class MyStack
- {
- Mutex mutexObj = new Mutex();
- public int Count { get; set; }
- static Stack<double> stack;
- public MyStack(int Count)
- {
- this.Count = Count;
- Thread AThread = new Thread(new ThreadStart(Input));
- AThread.Name = $"Thread Input";
- Thread BThread = new Thread(new ThreadStart(Checker));
- BThread.Name = $"Thread Checker";
- Thread CThread = new Thread(new ThreadStart(Output));
- CThread.Name = $"Thread Output";
- //start input thread
- AThread.Start();
- Task.Delay(10);
- //translate date
- BThread.Start();
- Task.Delay(15);
- //output default date
- CThread.Start();
- Task.Delay(10);
- }
- public void Input()
- {
- mutexObj.WaitOne(); // зупиняєм потік для отримання мютекса
- Console.WriteLine($"{Thread.CurrentThread.Name} Start");
- stack = new Stack<double>();
- for (int i = 0; i < Count; i++)
- {
- stack.Push(i);
- }
- mutexObj.ReleaseMutex(); // звільняєм мютекс
- }
- public void Output()
- {
- mutexObj.WaitOne(); // зупиняєм потік для отримання мютекса
- Console.WriteLine($"{Thread.CurrentThread.Name} Start");
- foreach (double b in stack)
- {
- Console.Write(b + "\t");
- }
- Console.WriteLine();
- mutexObj.ReleaseMutex(); // // звільняєм мютекс
- }
- public void Checker()
- {
- mutexObj.WaitOne(); // зупиняєм потік для отримання мютекса
- Console.WriteLine($"{Thread.CurrentThread.Name} Start");
- double to_check = 3;
- HashSet<double> checker = new HashSet<double>();
- foreach (double b in stack)
- checker.Add(b);
- if (checker.Contains(to_check))
- Console.WriteLine("Елемент " + to_check + " знайдено!");
- else
- Console.WriteLine("Елемент " + to_check + " НЕ знайдено!");
- mutexObj.ReleaseMutex(); // // звільняєм мютекс
- }
- }
- class Program
- {
- static void Main()
- {
- try
- {
- int size = 10;
- MyStack myStack = new MyStack(size);
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment