Pros and Cons of Multi-threading


I have started reading “Java Concurrency In Practice” which is the fourth one I am reading from this list of “Top 5 Java Books”. Really enjoyed the first three – Head First Java, Head First Design Patterns and Effective Java.
Putting down the summery of the first chapter from the book.

Pros:

Makes use of multiple processors: Increasing the clock speed is more difficult and expensive than increasing the number of cores, so the trend is to increase the cores. To avail the benefits of multi-core your program should be multi-threaded.

Modularity : A big task task can divided logically and can be handled by separate thread synchronously. Though we dont get a performance improvement in this case as the threads are executing in sequence one after the other we are achiving a better modularity here which helps in debugging.

Handling async requests: A server application which is multi-threaded – one thread for each requests – with synchrous I/O is easier to develop and less error prone than a single threaded application with asyncrhous I/O.

Better UI responses : The progress bar, pop-up messages, alerts, doing multiple operations etc. are not possible with a single threaded UI.

 

Cons/Risks:

Safety issues: Without proper synchronization a program where the order of execution is important can cause unexpected results with multiple threads. This is because multiple threads sharing the same memory allocated to the process.

Liveness issues: Deadlock, starvation & livelock are the culprits for multi-threaded programs to enter into non-live state.

Performance issues: Through one of the main reason for multi-threading is performance improvement, the switching and scheduling of threads introduces a slight performance impact. So in rare cases when this performance overload is more than executing the program in single thread prefer single-thread.

Reference: Java Concurrency in Action

Advertisement

One thought on “Pros and Cons of Multi-threading

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s