Printing all even and odd numbers with threads The original question is on careercup.


  Write a multi threaded C code with one thread printing all even numbers and the other all odd numbers. The output should always be in sequence
  ie. 0,1,2,3,4....etc


Now I want to use  C# for it.      

class Program
{
    static Object obj = new Object();
    static Thread t1;
    static Thread t2;
    static LinkedList&lt;int&gt; a = new LinkedList&lt;int&gt;();
    static void Main(string[] args)
    {
        for (int i = 0; i &lt; 10; i++)
        {
            a.AddLast(i);
        }

        t1 = new Thread(PrintOdd);
        t2 = new Thread(PrintEven);
        t1.Name = "Odd";
        t2.Name = "Even";
        t1.Start();
        t2.Start();
        t1.Join();
        t2.Join();

        Console.WriteLine("Done!");
        Console.Read();
    }

    private static void PrintOdd()
    {
        while (true)
        {
            if (a.Count == 0)
                break;
            lock (obj)
            {
                int x = a.First();
                if (x % 2 != 0)
                {
                    Console.WriteLine(Thread.CurrentThread.Name + " " + x);
                    a.RemoveFirst();
                }
            }
        }
    }

    private static void PrintEven()
    {
        while (true)
        { 
            lock (obj)
            {
                if (a.Count == 0)
                    break;
                int x = a.First();
                if (x % 2 == 0)
                {
                    Console.WriteLine(Thread.CurrentThread.Name + " " + x);
                    a.RemoveFirst();
                }
            }
        }
    }
}


Any improvements?
This looks very good to me, but I am not very advanced at C# myself.  I can give you some tips, though.

First, you should not use ambiguous names like t1 and t2.  You should use more descriptive names like EvenThread and OddThread instead.

Second, you can shorten this:

while (true)
{
  if (a.Count == 0)
    break;


Into this:

while (Numbers.Count &gt; 0)  // For PrintOdd()


And this:

while (Numbers.Count &gt; 1)  // For PrintEven()


Also, you could look into other forms of mutual exclusion techniques, such as semaphores and mutexes, instead of just using an Object.

This is an implementation with a SemaphoreSlim (only code shown is changed):

using System.Threading;

class Program
{
    static SemaphoreSlim ThreadLock = new SemaphoreSlim(1,1);
    static Thread Odd;
    static Thread Even;
    static LinkedList&lt;int&gt; Numbers = new LinkedList&lt;int&gt;();
    static void Main(string[] args)
    {
        for (int i = 0; i &lt; 10; i++)
        {
            Numbers.AddLast(i);
        }

        Odd = new Thread(PrintOdd);
        Even = new Thread(PrintEven);
        Odd.Name = "Odd";
        Even.Name = "Even";
        Odd.Start();
        Even.Start();
        Odd.Join();
        Even.Join();

        Console.WriteLine("Done!");
        Console.Read();
    }

    private static void PrintOdd()
    {
        while (Numbers.Count &gt; 0)
        {
            ThreadLock.Wait();

            int x = Numbers.First();
            if (x % 2 != 0)
            {
                Console.WriteLine(Thread.CurrentThread.Name + " " + x);
                Numbers.RemoveFirst();
            }

            ThreadLock.Release();
        }
    }

    private static void PrintEven()
    {
        while (Numbers.Count &gt; 1)
        {
            ThreadLock.Wait();

            int x = Numbers.First();
            if (x % 2 == 0)
            {
                Console.WriteLine(Thread.CurrentThread.Name + " " + x);
                Numbers.RemoveFirst();
            }

            ThreadLock.Release();
        }
    }
}