Sunday, 5 February 2017

Scala/Java Concurrency and Parallelism Interview Questions and Answers

Introduction

Before reading this post, Please go through my previous Scala Interview Questions and Answers posts:
In this post, we are going to discuss about some of Java and Scala Concurrency and Parallelism related Interview Questions and Answers.

Scala/Java Concurrency and Parallelism Interview Questions


  1. Why Sequential Programming is Bad?
  2. Why Concurrency Programming is Good?
  3. What are the two main categories of Concurrency Models available in JVM?
  4. What are the main Advantages of Shared State Concurrency Model?
  5. What are the main Drawbacks of Shared State Concurrency Model?
  6. What are the main Advantages of Akka/Scala Actors Concurrency Model or Scala's Future/Promise Model?
  7. How Akka Actor Concurrency Model works?
  8. What are the main Drawbacks of Actors Based Concurrency Model?
  9. How do we implement Shared State Concurrency Model in Java?
  10. How do we implement Locking Mechanism of  Shared State Concurrency Model in Java?
  11. Why do we need to implement Locking Mechanism or Synchronization Block for Shared State Concurrency Model in Java?
  12. What is a Future in Scala?
  13. What is a Promise Scala?
  14. Scala Future and Promise API?
  15. Differences between Scala Future and Promise?
  16. Differences between Scala Future and Java Thread?
  17. Differences between Scala Future and Java Future?
  18. What is an ExecutionContext?
  19. Advantages of Scala Parallel Collections?
  20. Differentiate between Concurrency and Parallelism?
  21. What is Scala Async API?
  22. Advantages of Scala Async API?
  23. Differences between Scala Future/Promise API and Async API?
                            NOTE:- Please support my Blog by clicking Ads to deliver more and more useful tutorials.
                            Scala/Java Concurrency and Parallelism Interview Questions and Answers
                            In this section, we will pickup each and every question from the list mentioned above and discuss in-detail with suitable examples(if required). If you want to understand these concepts in-depth with examples, please go through my previous posts in Scala/Java Tutorials section.


                            1. Why Sequential Programming is Bad?


                            These are the main drawbacks of Sequential Programming Model:
                            • Blocking Programming
                            • Wasting of Valuable Resources
                            • In-Efficient and In-Effective Use of Valuable Resources
                            • Synchronous Programming Model 
                            • Less Through Put
                            • Bad Performance
                            • More Waiting time
                            • Slow Response to the Client Requests   

                            2. Why Concurrency Programming is Good?

                            These are the main advantages of Concurrency Programming Model:
                            • Supports Non-Blocking Programming Model
                            • Efficient and Effective Use of Valuable Resources
                            • Asynchronous Programming Model
                            • More Through put
                            • Better Performance
                            • No Waiting time
                            • Instance Response to the Client Requests

                            3. What are the two main categories of Concurrency Models available in JVM?

                            There are two main categories in Concurrency Models:
                            • Shared Sate Concurrency Model
                            • No-Shared Sate Concurrency Model 
                            1. Actors Based Concurrency Model
                            2. Future/Promise Concurrency Model              

                            4. What are the main Advantages of Shared State Concurrency Model?

                            This Shared State Concurrency Model have the following few advantages:
                            • May be fast (If Developer is good in Understanding and Writing Multi Threading code)
                            • Using this model for longer time

                            5. What are the main Drawbacks of Shared State Concurrency Model?

                            This Shared State Concurrency Model have the following drawbacks:
                            • More difficult to write and understand the correct code
                            • Lot of boiler plate code
                            • Lots of Synchronization code to avoid race conditions
                            • Does not scale much in Distributed applications
                            • Tough to avoid Deadlock issues
                            • Tough to avoid Starvation issues
                            • Tough to avoid Live Locks
                            • Debugging is very tough
                            • Unit testing is bit tough
                            • More Resources utilization
                            • Very low level Concurrency Programming
                            • Lot of code to get and release Locks
                            • A Thread cannot create another Thread
                            • Threads are Heavy Weight
                            • Does not provide better Performance
                            • Supports only Concurrency, does NOT support True Parallelism.
                            • Does not support Non-Blocking functionality
                            • Context Switching overhead to support Concurrency

                            6. What are the main Advantages of Akka/Scala Actors Concurrency Model or Scala's Future/Promise Model?

                            This Actor Based Concurrency Model have the following few advantages:
                            • High Level Concurrency Programming
                            • No boiler plate code
                            • Easy to write Highly Scalable code
                            • Supports Distributed applications very well
                            • By default, Actors are Distributed
                            • More Type Safe
                            • No race conditions or dead lock issues
                            • Easy to avoid Starvation and Live Locks
                            • No Shared State
                            • Less Resources utilization
                            • An Actor can create or manager another Actor
                            • Unit Testing is very easy
                            • Easy to write and understand correct code
                            • No Locks and No Synchronization code
                            • Actors are Light weight 
                            • Supports High Performance
                            • Supports both Concurrency and True Parallelism very well
                            • Supports Non-Blocking functionality
                            • No Context Switching overhead
                            7. How Akka Actor Concurrency Model works?

                            In Akka/Scala Framework, Actor Concurrency model works
                            • By using Actors but not Objects
                            • Each Actor has it's MailBox
                            • No Direct communication between Actors
                            • One Actor send messages to another Actor Asynchronously
                            • Asynchronous Message Passing
                            • No Share State that means Actors never share their state with other Actors
                            • No Locking techniques. No competition to get locks
                            • An Actor's state can only change by itself
                            • Actor processes it's messages one by one only. That means one message at a time.

                            8. What are the main Drawbacks of Actors Based Concurrency Model?

                            In Akka/Scala Framework, Actor Concurrency model have the following few drawbacks or disadvantages or limitations:
                            • When our application needs shared state
                            • When our application needs Synchronous Communication
                            • When we cannot break our problem into smaller problems
                            Only in the above few cases, we cannot use Actor model.

                            9. How do we implement Shared State Concurrency Model in Java?

                            In Java, we implement Thread-based Shared state concurrency model by using
                            • Shared Mutable state
                            • Locking mechanism

                            10. How do we implement Locking Mechanism of  Shared State Concurrency Model in Java?


                            In Java, we implement Locking Mechanism of Shared state concurrency model by using one of the following two things
                            • Synchronization Block
                            • Locking API

                            11. Why do we need to implement Locking Mechanism or Synchronization Block for Shared State Concurrency Model in Java?

                            In Java, we implement Locking Mechanism or Synchronization Block for Shared state concurrency model to get the following two things
                            • Execute the code in the correct order
                            • To get correct visibility
                            • To avoid Data Inconsistency issues
                            • To avoid Dead Lock issues
                            • To avoid Starvation
                            • To avoid Live Locks

                            12. What is a Future in Scala?

                            In Scala, a Future is an object which represents a computation unit. It have some value at some point in future.

                            In other words, it is a programming construct in Scala used to write Concurrency programs very easily.


                            When a Future finishes it's computation, we call it as Future is Completed. However it may be completed either successfully or unsuccessfully.

                            If a Future is completed successfully, it has some computed value.  If its failed or completed unsuccessfully, it has some error.

                            NOTE:- Please go through my "Scala Future and Promise In Depth" tutorial to understand them very well.


                            Example-1:-
                            myFutureObj.onComplete {
                                case Success(value) => // do something
                                case Failure(error) => // Log error 
                            }
                            

                            13. What is a Promise in Scala?

                            In Scala, a Future is represents a Computation unit. A Promise is used to execute that Computation unit and write the result into a Future.

                            That mans, Promise is used to compute a value asynchronously and Future is used to read that value. One Promise is associated with only one Future.

                            NOTE:- Please go through my "Scala Future and Promise In Depth" tutorial to understand them very well.


                            14. Scala Future and Promise API?

                            Please go through my "Scala Future and Promise In Depth" tutorial to understand them very well.


                            15. Differences between Scala Future and Promise?

                            In Scala, Future and Promise have the following differences
                            • Promise is used to compute a value 
                            • Future is used to read that value
                            • Promise is used write the value
                            • Future is read-only object. 
                            • Once Promise is completed, we cannot change it's value. It become Immutable. 
                            • Once Future is completed, we cannot change it's value. It become Immutable. 
                            • Future is for Querying data and Promise is for Writing Data.
                            • In Simple words, Future is getter and Promise is setter.

                            16. What is ExecutionContext in Scala?

                            In Scala, as we discussed a Future is represents a Computation unit. A Promise is used to execute that Computation unit and write the result into a Future.

                            ExecutionContext is a place where our Future's Computation unit is executed.
                            Scala Language gives us a ready-made static global ExecutionContext in scala.concurrent.ExecutionContext.global.


                            NOTE:- In simple words, ExecutionContext is something similar to a Thread Pool (Java Guys also can understand this).

                            NOTE:- Please support my Blog by clicking Ads to deliver more and more useful tutorials.

                            IN PROGRESS

                            Will answer rest of the questions soon.


                            That’s it all about "Scala/Java Concurrency and Parallelism related Interview Questions and Answers". We will discuss some more Scala Interview Questions in my coming posts.

                            Please drop me a comment if you like my post or have any issues/suggestions.

                            I love your valuable comments so much.