Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Threading;
- namespace ConsoleApplication1
- {
- public class Program
- {
- static object obj = new object();
- static string wiadomosc;
- public static void Main()
- {
- Thread czytaj = new Thread(new ThreadStart(Czytajacy));
- czytaj.Name = "Watek czytajacy";
- Thread pisz = new Thread(new ThreadStart(Piszacy));
- pisz.Name = "Watek piszacy";
- pisz.Start();
- czytaj.Start();
- //pisz.Start();
- Console.ReadKey();
- }
- public static void Czytajacy()
- {
- Monitor.Enter(obj);
- if (wiadomosc == null)
- {
- Console.WriteLine("{0}: brak wiadomosci", Thread.CurrentThread.Name);
- Console.WriteLine("{0}: czekaj", Thread.CurrentThread.Name);
- Monitor.Wait(obj);
- }
- Console.WriteLine("{0}: {1}", Thread.CurrentThread.Name, wiadomosc);
- Monitor.Exit(obj);
- }
- public static void Piszacy()
- {
- Monitor.Enter(obj);
- wiadomosc = "Pozdrowienia!";
- if (wiadomosc != null)
- {
- Console.WriteLine("{0}: Wiadomość została wyslana", Thread.CurrentThread.Name);
- Monitor.Pulse(obj);
- }
- Monitor.Exit(obj);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement