-3

Task code creates 14 thread in process -

enter image description here

  Task.Run(()=>abc());
     Task.Run(() => abc());
     Task.Run(() => abc());
     Task.Run(() => abc());
     Task.Run(() => abc());
     Console.Read();

Thread code creates 10 thread in process
enter image description here

Thread t = new Thread(abc); t.Start(); Thread t1 = new Thread(abc); t1.Start(); Thread t2 = new Thread(abc); t2.Start(); Thread t3 = new Thread(abc); t3.Start(); Thread t4 = new Thread(abc); t4.Start();

jps
  • 20,041
  • 15
  • 75
  • 79
Sai Sherlekar
  • 1,804
  • 1
  • 17
  • 18
  • How are you proving this for your self? Also Tasks aren't wrappers for threads, though the Task scheduler may liberate a thread pool thread if it deems necessary and/or feels like it – TheGeneral Sep 03 '20 at 05:02
  • 1
    Tasks don't create any thread directly, they use thread available in thread pool. – Mojtaba Tajik Sep 03 '20 at 05:06
  • 2
    `Task.Run()` is normally for CPU-bound operations so yes you would expect a thread to be used. However, not all `Task`s require threads such as I/O-bound IOCP code operations where you might find no thread at all. For these types of operations you wouldn't use `Task.Run`. https://blog.stephencleary.com/2013/11/there-is-no-thread.html –  Sep 03 '20 at 05:15

1 Answers1

0

Task.Run(...) schedules actions/tasks to run on the threadpool:

Queues the specified work to run on the thread pool and returns a Task object that represents that work. A cancellation token allows the work to be cancelled if it has not yet started.

https://learn.microsoft.com/en-us/dotnet/api/system.threading.tasks.task.run?view=netcore-3.1#System_Threading_Tasks_Task_Run_System_Action_

Also see:

https://learn.microsoft.com/en-us/dotnet/api/system.threading.threadpool?view=netcore-3.1

sommmen
  • 6,570
  • 2
  • 30
  • 51