任务忽略 Thread.Sleep

发布于 2024-09-29 21:56:24 字数 797 浏览 0 评论 0原文

试图掌握TPL。

只是为了好玩,我尝试创建一些具有随机睡眠的任务,以查看它是如何处理的。我的目标是即发即忘模式..

static void Main(string[] args)
    {
        Console.WriteLine("Demonstrating a successful transaction");

        Random d = new Random();
        for (int i = 0; i < 10; i++)
        {
            var sleep = d.Next(100, 2000);


            Action<int> succes = (int x) =>
            {
                Thread.Sleep(x);
                Console.WriteLine("sleep={2}, Task={0}, Thread={1}: Begin successful transaction",
                   Task.CurrentId, Thread.CurrentThread.ManagedThreadId, x);
            };

            Task t1 = Task.Factory.StartNew(() => succes(sleep));
        }
        Console.ReadLine();
    }

但我不明白为什么它将所有行输出到控制台而忽略睡眠(随机)

有人可以向我解释一下吗?

trying to grasp the TPL.

Just for fun I tried to create some Tasks with a random sleep to see how it was processed. I was targeting a fire and forget pattern..

static void Main(string[] args)
    {
        Console.WriteLine("Demonstrating a successful transaction");

        Random d = new Random();
        for (int i = 0; i < 10; i++)
        {
            var sleep = d.Next(100, 2000);


            Action<int> succes = (int x) =>
            {
                Thread.Sleep(x);
                Console.WriteLine("sleep={2}, Task={0}, Thread={1}: Begin successful transaction",
                   Task.CurrentId, Thread.CurrentThread.ManagedThreadId, x);
            };

            Task t1 = Task.Factory.StartNew(() => succes(sleep));
        }
        Console.ReadLine();
    }

But I don't understand why it outputs all lines to the Console ignoring the Sleep(random)

Can someone explain that to me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

早茶月光 2024-10-06 21:56:24

重要提示:

TPL 默认的 TaskScheduler 不保证每个任务有一个线程 - 一个线程可用于处理多个任务。

调用 Thread.Sleep 可能会影响其他任务的性能。

您可以使用 TaskCreationOptions.LongRunning 提示构建您的任务,这样 TaskScheduler 将为该任务分配一个专用线程,并且它将安全地阻止它。

Important:

The TPL default TaskScheduler does not guarantee Thread per Task - one thread can be used for processing several tasks.

Calling Thread.Sleep might impact other tasks performance.

You can construct your task with the TaskCreationOptions.LongRunning hint this way the TaskScheduler will assign a dedicated thread for the task and it will be safe to block on it.

淡淡離愁欲言轉身 2024-10-06 21:56:24

您的代码使用 i 的值而不是生成的随机数。它不会忽略睡眠,而是每次迭代睡眠 0 到 10 毫秒。

尝试:

Thread.Sleep(sleep);

Your code uses the value of i instead of the generated random number. It does not ignore the sleep but rather sleeps between 0 and 10ms each iteration.

Try:

Thread.Sleep(sleep);
再浓的妆也掩不了殇 2024-10-06 21:56:24

这句话

任务 t1 = Task.Factory.StartNew(() => 成功(睡眠));

将创建任务并自动启动它,然后将在 for 内部再次迭代,而不等待任务结束其进程。因此,当第二个任务创建并执行时,第一个任务可能已完成。我的意思是你不是在等待任务结束:

你应该尝试

Task t1 = Task.Factory.StartNew(() => succes(sleep)); 
t1.Wait();

The sentence

Task t1 = Task.Factory.StartNew(() => succes(sleep));

Will create the Task and automatically start it, then will iterate again inside the for, without waiting the task to end its process. So when the second task is created and executed, the first one may be finished. I mean you are not waiting for the tasks to end:

You should try

Task t1 = Task.Factory.StartNew(() => succes(sleep)); 
t1.Wait();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文