Creating threads - Task.Factory.StartNew vs new Thread()

前端 未结 4 1489
感动是毒
感动是毒 2020-11-29 16:48

I am just learning about the new Threading and Parallel libraries in .Net 4

In the past I would create a new Thread like so (as an example):

DataInTh         


        
4条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-29 17:20

    Your first block of code tells CLR to create a Thread (say. T) for you which is can be run as background (use thread pool threads when scheduling T ). In concise, you explicitly ask CLR to create a thread for you to do something and call Start() method on thread to start.

    Your second block of code does the same but delegate (implicitly handover) the responsibility of creating thread (background- which again run in thread pool) and the starting thread through StartNew method in the Task Factory implementation.

    This is a quick difference between given code blocks. Having said that, there are few detailed difference which you can google or see other answers from my fellow contributors.

提交回复
热议问题