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

后端 未结 9 988
生来不讨喜
生来不讨喜 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:53

    You should use multithreading when you want to perform heavy operations without "blocking" the flow.
    Example in UIs where you do a heavy processing in a background thread but the UI is still active.

    If the threads execute mutually exclusive tasks it is the best since there is no overhead for synchronization among threads needed

    0 讨论(0)
  • 2020-12-23 09:56

    This article gives very good reasons: https://marcja.wordpress.com/2007/04/06/four-reasons-to-use-multithreading/

    To summarize, the reasons are:

    • Keep your program responsive.
    • Make better use of your CPU. CPU may be blocked by IO or other stuff. While waiting, why not letting other threads use it
    • Multiple threads can be scheduled to multiple CPU cores
    • Some problems are naturally to be solved by multi-threading. Such solution can simplify your code.
    0 讨论(0)
  • 2020-12-23 09:59

    You can use multithreading if the tasks can be broken down which can be executed in parallel. Like produce and consume , Validate and save , Read and Validate.

    For the second question , Yes, it is beneficial for make a program into Multi threading if they are executing independent tasks.

    0 讨论(0)
提交回复
热议问题