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

JavaMultithreading

Java Problem Overview


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

Java Solutions


Solution 1 - Java

Q: When should you use multithreading?

A: "Your question is very broad. There are few non-trivial systems where the functionality can be met simply, quickly and reliably with only one thread. For example: [pick out a typical system that the target company sells and pick out a couple aspects of its function that would be better threaded off - heavy CPU, comms, multi-user - just pick out something likely & explain].

Q: Would multithreading be beneficial if the different threads execute mutually independent tasks?

A: "Depends on what you mean by 'executing tasks'. Multithreading would surely be beneficial if the threads process mutually independent data in a concurrent fashion - it reduces requirements for locks and probabilty of deadlocks increases in a super-linear fashion with the number of locks. OTOH, there is no issue with threads executing the same code, this is safe and very common."

Solution 2 - Java

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

Solution 3 - Java

Multithreading is a way to introduce parallelness in your program. In any case if there can be parallel paths (parts which do not depend on result from a other part) in your program, use can make use of it.

Specially with all these multiple core machines now days, this is a feature which one should exploit.

Some examples would be processing of large data where you can divide it in chunks and get it done in multiple threads, file processing, long running I/O works like network data transfers etc.

To your second question, it would be best if the tasks are mutually independent - reasons

  • no shared data means no contentions
  • no need for any ordered processing (dependency), so each thread can work when have resources
  • more easy to implement

Solution 4 - Java

You should definitely use multithreading in GUI applications when you invoke time consuming tasks from the main event loop. Same applies for server application that might block while doing the I/O.

For the second question, it is usually yes when you have machine with multiple CPU cores. In this case these independent tasks can be executed in parallel.

Solution 5 - Java

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.

Solution 6 - Java

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.

Solution 7 - Java

In general, multithreading is used in cases where execution time is throttled/bottlenecked by the CPU as opposed to other areas such as IO. The second question is really quite subjective to the circumstance. For example if they are mutually independent but both do heavy IO, you might not necessarily get a large gain.

Solution 8 - Java

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.

Solution 9 - Java

>When should you use multithreading?

Multithreading is a process of executing multiple threads simultaneously. You should use multithreading when you can perform multiple operations together so that it can save time.

> Would multithreading be beneficial if the different threads execute mutually independent tasks?

it is usually yes. Multithreading would usually be beneficial if the different threads execute mutually independent tasks so that it doesn't affect other threads if exception occur in a single thread.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGrowinManView Question on Stackoverflow
Solution 1 - JavaMartin JamesView Answer on Stackoverflow
Solution 2 - JavaCratylusView Answer on Stackoverflow
Solution 3 - JavaNrjView Answer on Stackoverflow
Solution 4 - JavaJooMingView Answer on Stackoverflow
Solution 5 - Javajava_mouseView Answer on Stackoverflow
Solution 6 - JavaboleiView Answer on Stackoverflow
Solution 7 - JavaMike KwanView Answer on Stackoverflow
Solution 8 - JavaPrinceView Answer on Stackoverflow
Solution 9 - JavarashedcsView Answer on Stackoverflow