Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Threading;
- namespace ParallelWork;
- class Program
- {
- static object locker = new object();
- static int sharedData;
- static void Main(string[] args)
- {
- var readerThread = new Thread(Reader);
- var writerThread = new Thread(Writer);
- readerThread.Start();
- writerThread.Start();
- }
- static void Reader()
- {
- while (true)
- {
- lock (locker)
- {
- Thread.Sleep(2000);
- Console.WriteLine("ReaderThread {0} считал данные: {1}", Thread.CurrentThread.ManagedThreadId, sharedData);
- }
- }
- }
- static void Writer()
- {
- while (true)
- {
- lock (locker)
- {
- Thread.Sleep(2000);
- sharedData++;
- Console.WriteLine("WriterThread {0} записал данные: {1}", Thread.CurrentThread.ManagedThreadId, sharedData);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement