郭成俊 2019-06-28 09:56 采纳率: 100%
浏览 228

C#线程初始化顺序相关问题

我运行了以下代码块,在main方法下的for循环中会初始化6个线程,从thread1依次至thread6,但在命令行中显示线程生成顺序与for循环运行的顺序不一致

代码块:

using System;
using System.Threading;
using static System.Console;
using static System.Threading.Thread;

namespace Chapter2.Recipe3
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 1; i <= 6; i++)
            {
                string threadName = "Thread " + i;
                int secondsToWait = 2 + 2 * i;
                var t = new Thread(() => AccessDatabase(threadName, secondsToWait));
                t.Start();
            }
        }

        static SemaphoreSlim _semaphore = new SemaphoreSlim(4);

        static void AccessDatabase(string name, int seconds)
        {
            WriteLine($"{name} waits to access a database");
            _semaphore.Wait();
            WriteLine($"{name} was granted an access to a database");
            Sleep(TimeSpan.FromSeconds(seconds));
            WriteLine($"{name} is completed");
            _semaphore.Release();
        }
    }
}

命令行结果:

Thread4 waits to access database
Thread3 waits to access database
Thread1 waits to access database
Thread6 waits to access database
Thread2 waits to access database
Thread5 waits to access database
Thread2 was granted an access to a database
Thread1 was granted an access to a database
Thread4 was granted an access to a database
Thread3 was granted an access to a database
Thread1 is completed
Thread5 was granted an access to a database
Thread2 is completed
Thread6 was granted an access to a database
Thread3 is completed
Thread4 is completed
Thread5 is completed
Thread6 is completed

Press any key to continue...

想请教大家为什么线程生成的顺序与for循环里面定义的顺序不一致,谢谢!

  • 写回答

1条回答

  • WeiYiYG 2019-07-19 16:52
    关注
     static void Main(string[] args)
    
    {
    
                int i = 5;
    
                Thread thread = new Thread(() => {
    
                    Console.WriteLine("i=" + i);
    
                });
    
                thread.Start();
    
                i = 6;
    
                List<string> Ts = new List<string>();
    
                for (int j = 0; j < 10; j++)//不一定按照顺序1到10输出
    
                {
    
                    Ts.Add(j.ToString());
    
                    Thread threadj = new Thread(() => {
    
                        Console.WriteLine("j=" + j);
    
                    });
    
                    threadj.Start();
    
                }
    
    
    
                //改成用 ParameterizedThreadStart 就不容易弄错。
    
                Thread threadP = new Thread(RunP);
    
                threadP.Start("a");
    
               // 改造成 lambda 形式:
    
                Thread threadPl = new Thread((obj) => { Console.WriteLine(obj); });
    
                //把有问题的代码改造如下: 
    
    
    
                int q = 5;
    
                Thread threadq = new Thread((obj) => { Console.WriteLine("q=" + obj); });
    
                threadq.Start(q);
    
                q = 6;
    
    
    
                for (int w = 0; w < 10; w++)
    
                {
    
                    Thread threadw = new Thread((item) => { Console.WriteLine("w=" + item); });
    
                    threadw.Start(w);
    
                }
    
    }
    
    
    
    static void RunP(object obj)
    
    {
    
           Console.WriteLine(obj);
    
    }
    
    
    
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题