Multiple threads calling same method in C#

2020-02-13 01:38发布

I have the following C# code snippet in which I have simulated my problem. In this program I have a Service function that call ReadRooms method. Now I am calling the service method on different threads. I was expecting that both ServiceCall and ReadRooms method will fired equally but I am getting below result that is not correct.

enter image description here

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        public static void ReadRooms(int i)
        {
            Console.WriteLine("Reading Room::" + i);
            Thread.Sleep(2000);
        }
        public static void CallService(int i)
        {
            Console.WriteLine("ServiceCall::" + i);
            ReadRooms(i);

        }
        static void Main(string[] args)
        {
            Thread[] ts = new Thread[4];
            for (int i = 0; i < 4; i++)
            {
                ts[i] = new Thread(() =>
                    {
                        int temp = i;
                        CallService(temp);


                    });
                ts[i].Start();
            }
            for (int i = 0; i < 4; i++)
            {
                ts[i].Join();
            }
            Console.WriteLine("done");
            Console.Read();

        }
    }
}

3条回答
【Aperson】
2楼-- · 2020-02-13 02:06

You are still 'capturing the loop variable'. You are creating a temp but too late, when i is already captured.

Try this:

for (int i = 0; i < 4; i++)
{
   int temp = i;              // outside the lambda
   ts[i] = new Thread(() =>
   {
        //int temp = i;       // not here
        CallService(temp);
   });
   ts[i].Start();
}
查看更多
等我变得足够好
3楼-- · 2020-02-13 02:06

Your thread action is closing over the variable i instead of its current value. You therefore have a race between the thread reading i and the increment in the for loop. You can pass it as a parameter instead:

ts[i] = new Thread(index =>
{
    CallService((int)index);
});
ts[i].Start(i);

alternatively you can move the copy of temp to inside the loop instead of the thread action:

for (int i = 0; i < 4; i++)
{
    int temp = i;
    ts[i] = new Thread(() =>
    {
        CallService(temp);
    });
    ts[i].Start();
}
查看更多
该账号已被封号
4楼-- · 2020-02-13 02:19

You should put this line

int temp = i;

before the Thread creation

for (int i = 0; i < 4; i++)
{
    int temp = i;
    ts[i] = new Thread(() => CallService(temp));
    ts[i].Start();
}

This way you will create a local copy of i that will be used by the lambda expression.

查看更多
登录 后发表回答