When should you use multithreading? And would multi threading be beneficial if the different threads execute mutually independent tasks?

后端 未结 9 986
生来不讨喜
生来不讨喜 2020-12-23 09:06

This were the only two questions I couldn\'t answer in the interview I got rejected from last night.

9条回答
  •  有刺的猬
    2020-12-23 09:43

    Multithreading is used when we can divide our job into several independent parts. For example, suppose you have to execute a complex database query for fetching data and if you can divide that query into sereval independent queries, then it will be better if you assign a thread to each query and run all in parallel. In that way, your final result output will be faster. Again, this is an example when you have the leverage to run mutliple database queries.

    And to answer your second question, it is better to have threads for independent tasks. Otherwise, you will have to take care of synchronization, global variables, etc.

提交回复
热议问题