I’m learning java concurrency under the hood and I’ve read some java article and videos regarding multi threading and concurrency and just can’t seem to put them all together
Here is the gist of what i’ve understand so far (Please correct me if im wrong!) :
- CPU has physical core and logical core. Logical core in essence is physical core * the number of threads that can run on each cores
- Java has a Request per Thread model, therefore for each request will take 1 of these logical core
- Hence if the cpu has 8 logical core, jvm can run 8 process parallelly (not concurrent!)
- Concurrency refers to a process where a single logical core can handle multiple request, assume a request will do a blocking I/O call, rather than waiting for the I/O call, the thread can be freed to be used to handle another request. this is what it meant to be concurrent
With all that in mind
Now i guess this is where its gets confusing
Suppose i have 8 logical core, and a spring web mvc server that can receive api call. In this api call, we will do an never ending blocking I/O operation
Does this mean that
- I can only hit at maximum 8 times before another request will be queued / delayed since all the threads are used?
The next one would be an improvement on the spring web mvc, rather than doing the I/O operation on the main thread, i want to do it in a non blocking way. which means i will spawn a new thread for each of the api call. Does this mean that
- I can only hit at maximum 4 times before another request will be queued / delayed since all the threads are used? (4 main thread, 4 from the blocking I/O call)
Taking a step further, rather than doing the I/O call on the new thread in a blocking manner, i will now use CompletableFuture / Future so that it’s asynchronous and non-blocking.
x = CompletableFuture.supplyAsync(() -> {
// Long I/O call
})
// do some operation
x.join();
My question here will be
- After the supplyAsync part, the process will continue until we come to x.join(); while waiting for x.join() will the thread be blocking? or will the thread be freed and context switching will happen so that the thread can be used to serve another request?
If the answer to the question number 3 is the thread will be blocking, doesn’t that mean the only difference from the new thread I/O blocking call is that when we use completableFuture we can // do some operation
before the blocking call?
For the question number 3 i assume that once we get to the blocking part of the code (.get() for Future / .join() completableFuture) the thread will be freed and can handle another request
javanoob is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.