0

We have a multi threaded application that uses synchronous methods. Is there a performance difference between the two methods?

        public void RunSleep()
        {
            Thread.Sleep(3000);
        }
        public void RunTask()
        {
            var task = Task.Run(() =>
            {
                Thread.Sleep(3000);
            });
            task.Wait();
        }

Thread.Sleep is supposed to symbolise an HTTP request.

While I understand that refactoring it to be an asynchronous method would be optimal, is there a benefit to using the second version?

EDIT: to specify, my question was if wrapping a long running and synchronous method in a task results in more efficient multi threading, based on this thread this thread.

Relevant quote:

Use Thread.Sleep when you want to block the current thread.

and

Use Task.Delay when you want a logical delay without blocking the current thread.

Ferenc
  • 527
  • 3
  • 6
  • 2
    https://ericlippert.com/2012/12/17/performance-rant/ – mjwills Aug 12 '19 at 10:42
  • Of those two options, I'd strongly encourage the first. The second, for example, may wait for considerably longer than 3 seconds. – mjwills Aug 12 '19 at 10:43
  • Even if they both took the same time, what's the point of the second block of code? You are just doing the same but adding some extra overhead and making it more difficult to read. – Isma Aug 12 '19 at 10:45
  • 2
    Well, the second version will deadlock in asp net/GUI apps and the first won't. – spender Aug 12 '19 at 10:46
  • Why the question? `RunTask` is a rather convoluted way of blocking and quite wasteful. Are you trying to solve some problem and think that maybe calling `Sleep` inside a task is the solution? – Panagiotis Kanavos Aug 12 '19 at 10:47
  • 2
    `RunTask` is bad for a couple of reasons 1) it blocks a threadpool thread that could serve another background job. 2) it results in CPU waste because `Wait()` like all blocking operations *except* `Sleep()` starts with a spinwait, to avoid expensive thread switching. – Panagiotis Kanavos Aug 12 '19 at 10:51
  • 4
    `Thread.Sleep is supposed to symbolise an HTTP request.` that's a bad example then. Post the *actual* HTTP code. Given that HttpClient methods are all asynchronous, trying to make them run synchronously is a bad idea. Check [Don't Block on Async Code](https://blog.stephencleary.com/2012/07/dont-block-on-async-code.html) – Panagiotis Kanavos Aug 12 '19 at 10:52
  • 3
    `Use Task.Delay when you want a logical delay without blocking the current thread.` Your use of `Wait()` offsets that theoretical benefit. Plus, you aren't even using `Task.Wait` anyway - you just tell a second thread to wait 3 seconds and then your main thread waits for that second thread. Now you have **two threads** doing nothing... Nonetheless, based on the limited context you have shown us - you should use your first code sample. – mjwills Aug 12 '19 at 11:05
  • @spender Can you tell me how? – Ferenc Aug 12 '19 at 11:11
  • 1
    I don't think you asked the real (deeper) question, why do you have "a multi threaded application that uses synchronous methods" ? – H H Aug 12 '19 at 11:46

2 Answers2

1
Thread.Sleep is supposed to symbolise an HTTP request

In that case it would be a clever decision to stick with an asynchronous method because all HttpClient Methods are asynchronous

my question was if wrapping a long running and synchronous method in a task results in more efficient multi threading, based on this thread

I guess that would be a bad idea

For further reading, you can take a look at this nice article.

0

I understand that Task.Wait() blocks the thread all the same, all I get from using them is some overhead. Thank you for the answers and discussion.

Ferenc
  • 527
  • 3
  • 6