public static void Main()
Console.WriteLine("Hello World");
MyCounter counterOne = new MyCounter();
MyCounter counterTwo = new MyCounter();
Console.WriteLine("First some static counts");
MyCounter.CountAndPrint();
MyCounter.CountAndPrint();
MyCounter.CountAndPrint();
Console.WriteLine("Then some non-static counts");
counterOne.PublicCountAndPrint();
counterTwo.PublicCountAndPrint();
counterOne.PublicCountAndPrint();
counterTwo.PublicCountAndPrint();
Console.WriteLine("Then we will test it with threads");
Thread tid1 = new Thread(new ThreadStart(Thread1));
Thread tid2 = new Thread(new ThreadStart(Thread2));
Console.WriteLine("Thread1: Hello World");
MyCounter counterOne = new MyCounter();
MyCounter counterTwo = new MyCounter();
Console.WriteLine("Thread1: First some static counts");
MyCounter.CountAndPrint("Thread1: ");
MyCounter.CountAndPrint("Thread1: ");
MyCounter.CountAndPrint("Thread1: ");
Console.WriteLine("Thread1: Then some non-static counts");
counterOne.PublicCountAndPrint("Thread1: ");
counterTwo.PublicCountAndPrint("Thread1: ");
counterOne.PublicCountAndPrint("Thread1: ");
counterTwo.PublicCountAndPrint("Thread1: ");
Console.WriteLine("Thread2: Hello World");
MyCounter counterOne = new MyCounter();
MyCounter counterTwo = new MyCounter();
Console.WriteLine("Thread2: First some static counts");
MyCounter.CountAndPrint("Thread2: ");
MyCounter.CountAndPrint("Thread2: ");
MyCounter.CountAndPrint("Thread2: ");
Console.WriteLine("Thread2: Then some non-static counts");
counterOne.PublicCountAndPrint("Thread2: ");
counterTwo.PublicCountAndPrint("Thread2: ");
counterOne.PublicCountAndPrint("Thread2: ");
counterTwo.PublicCountAndPrint("Thread2: ");
private static object objectLock = new object();
private static int counter = 0;
public static void CountAndPrint()
Console.WriteLine(counter);
public static void CountAndPrint(string prefix)
Console.WriteLine(prefix + counter.ToString());
public void PublicCountAndPrint()
Console.WriteLine(counter);
public void PublicCountAndPrint(string prefix)
Console.WriteLine(prefix + counter.ToString());