No synchronization example – full chaos during execution…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Threading; namespace ThreadsSync { class Program { static void Main(string[] args) { StartManyThreadsNoSynchronization(); } static void StartManyThreadsNoSynchronization() { Printer p = new Printer(); Thread[] threads = new Thread[10]; for (int i = 0; i < 10; i++) { threads[i] = new Thread(new ThreadStart(p.PrintNumbers)); threads[i].Name = string.Format("Worker thread #{0}",i); } foreach (Thread t in threads) t.Start(); Console.ReadLine(); } } public class Printer { public void PrintNumbers() { for (int i = 0; i < 10; i++) { Random r = new Random(); Thread.Sleep(1000 * r.Next(5)); Console.Write("{0},",i); } Console.WriteLine(); } } } |
Synchronized with lock example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Threading; namespace ThreadsSync { class Program { static void Main(string[] args) { StartManyThreads(); } static void StartManyThreads() { Printer p = new Printer(); Thread[] threads = new Thread[10]; for (int i = 0; i < 10; i++) { threads[i] = new Thread(new ThreadStart(p.PrintNumbers)); threads[i].Name = string.Format("Worker thread #{0}", i); } foreach (Thread t in threads) t.Start(); Console.ReadLine(); } } public class Printer { private object threadLock = new object(); public void PrintNumbers() { lock (threadLock) { for (int i = 0; i < 10; i++) { Random r = new Random(); Thread.Sleep(1000 * r.Next(5)); Console.Write("{0},", i); } Console.WriteLine(); } } } } |
Synchronized with Monitor
System.Threading.Monitor has more options like Monitor.Pulse(), Monitor.PulseAll()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Threading; namespace ThreadsSync { class Program { static void Main(string[] args) { StartManyThreads(); } static void StartManyThreads() { Printer p = new Printer(); Thread[] threads = new Thread[10]; for (int i = 0; i < 10; i++) { threads[i] = new Thread(new ThreadStart(p.PrintNumbers)); threads[i].Name = string.Format("Worker thread #{0}", i); } foreach (Thread t in threads) t.Start(); Console.ReadLine(); } } public class Printer { private object threadLock = new object(); public void PrintNumbers() { Monitor.Enter(threadLock); try { for (int i = 0; i < 10; i++) { Random r = new Random(); Thread.Sleep(1000 * r.Next(5)); Console.Write("{0},", i); } Console.WriteLine(); } finally { Monitor.Exit(threadLock); } } } } |
Also possible synchronization with System.Threading.Interlocked and [Synchronization] attribute, which is below, class should be derived like
1 |
public class Printer:ContextBoundObject |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Threading; using System.Runtime.Remoting.Contexts; namespace ThreadsSync { class Program { static void Main(string[] args) { StartManyThreads(); } static void StartManyThreads() { Printer p = new Printer(); Thread[] threads = new Thread[10]; for (int i = 0; i < 10; i++) { threads[i] = new Thread(new ThreadStart(p.PrintNumbers)); threads[i].Name = string.Format("Worker thread #{0}", i); } foreach (Thread t in threads) t.Start(); Console.ReadLine(); } } [Synchronization] public class Printer:ContextBoundObject { private object threadLock = new object(); public void PrintNumbers() { for (int i = 0; i < 10; i++) { Random r = new Random(); Thread.Sleep(1000 * r.Next(5)); Console.Write("{0},", i); } Console.WriteLine(); } } } |