- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Threading;
- namespace ThreadSynchronizationIssue
- {
- class Program
- {
- static int count = 0;
- static void Main(string[] args)
- {
- Thread thread1 = new Thread(Increment);
- Thread thread2 = new Thread(Increment);
- thread1.Start();
- Thread.Sleep(500);
- thread2.Start();
- }
- static void Increment()
- {
- while (true)
- {
- count++;
- Console.WriteLine("theadID {1} - This is SynchronizationIssue: {0}", count, Thread.CurrentThread.ManagedThreadId);
- Thread.Sleep(1000);
- }
- }
- }
- }
Output:
theadID 3 - This is SynchronizationIssue: 1
theadID 4 - This is SynchronizationIssue: 2
theadID 3 - This is SynchronizationIssue: 3
theadID 4 - This is SynchronizationIssue: 4
theadID 3 - This is SynchronizationIssue: 5
theadID 4 - This is SynchronizationIssue: 6
theadID 3 - This is SynchronizationIssue: 7
theadID 4 - This is SynchronizationIssue: 8
theadID 3 - This is SynchronizationIssue: 9
theadID 4 - This is SynchronizationIssue: 10
theadID 3 - This is SynchronizationIssue: 11
theadID 4 - This is SynchronizationIssue: 12
theadID 3 - This is SynchronizationIssue: 13
theadID 4 - This is SynchronizationIssue: 14
theadID 3 - This is SynchronizationIssue: 15