Here is the question:
Say there are 3 lists l1, l2 & l3 of same length. Three threads accessing three lists. Say T1 -> l1, T2 ->l2 & T3 ->l3. It should print in the order say first element of 1st then first element of 2nd list and then first element of 3rd list. Then second element of 1st then second element of 2nd list and then second element of 3rd list.
What I tried:
private static readonly Object obj = new Object();
static List<string> list1 = new List<string> { "1", "2", "3", "4" };
static List<string> list2 = new List<string> { "a", "b", "c", "d" };
static List<string> list3 = new List<string> { "*", "+", "-", "?" };
static int i = 0;
static void Main(string[] args)
{
Thread t1 = new Thread(() => PrintItem());
t1.Name = "Print1";
Thread t2 = new Thread(() => PrintItem());
t2.Name = "Print2";
Thread t3 = new Thread(() => PrintItem());
t3.Name = "Print3";
t1.Start();
t2.Start();
t3.Start();
t1.Join();
t2.Join();
t3.Join();
Console.Read();
}
private static void PrintItem()
{
while (true)
{
lock (obj)
{
if (i >= list1.Count)
break;
Console.WriteLine(Thread.CurrentThread.Name + " " + list1[i]);
Console.WriteLine(Thread.CurrentThread.Name + " " + list2[i]);
Console.WriteLine(Thread.CurrentThread.Name + " " + list3[i]);
i++;
}
}
}
The output is right but it doesn't use three threads. Correct the code please.