This takes about 2.7 seconds to complete, an extended latency to gather some information. On this instance, every name to RestTemplate blocks the thread it’s operating on till the decision returns some information after which fingers the thread over to the subsequent name to RestTemplate. It really works this fashion as a result of RestTemplate, by nature, is a synchronous blocking operation. If you wish to know the way blocking and non-blocking operations work, there’s a detailed clarification right here.
After studying this, Tracey decides that she will use a few of her system’s idle threads to make a number of blocking calls on the identical time. It’s good to clear up a misunderstanding right here: that is known as parallelism, not concurrency.
GOTCHA: Parallelism and concurrency should not the identical
Even when this isn’t a routine particular matter, there’s a frequent false impression about parallelism and concurrency. These phrases are sometimes used interchangeably, however they symbolize totally different ideas.
Let’s take a quick journey into the realms of sequential locking, concurrency, and parallelism by imagining a bustling workplace state of affairs, the place a diligent worker is actively serving to clients.
Sequential/Blocking (no concurrency or parallelism):
In our imaginary workplace, this habits would appear like the worker serving to one buyer at a time. Whereas the primary buyer fills out the shape, the worker waits and doesn’t serve some other clients.
A single-threaded program works in a sequential/blocking method, the place duties are executed one after one other with none simultaneous or parallel execution.
Parallelism:
This includes the worker calling a colleague for assist. Now, each clients are served concurrently by totally different workers. Every worker works independently on the assigned job, resulting in true parallel execution.
In a multithreaded or multithreaded program, duties truly run concurrently on a number of processors or cores. For instance, two calls to RestTemplate will be made on the identical time, however they are going to use and block totally different threads, with out sharing their assets.