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

前端 未结 4 1490
感动是毒
感动是毒 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:12

    In the first case you are simply starting a new thread while in the second case you are entering in the thread pool.

    The thread pool job is to share and recycle threads. It allows to avoid losing a few millisecond every time we need to create a new thread.

    There are a several ways to enter the thread pool:

    • with the TPL (Task Parallel Library) like you did
    • by calling ThreadPool.QueueUserWorkItem
    • by calling BeginInvoke on a delegate
    • when you use a BackgroundWorker

提交回复
热议问题