Reputation: 21
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!) :
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
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
Thread t1 = new Thread(() -> {
// Blocking I/O call
})
t1.start();
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
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
(refer to the 2nd improvement) 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.
Note : Sorry for my bad english!
Upvotes: 0
Views: 565
Reputation: 339837
CPU has physical core and logical core.
Not always. Only CPU chips with a simultaneous multithreading (SMT) technology like Intel’s Hyper-Threading will appear to offer two logical cores per real physical core.
Basically this technology involves two sets of registers that hold the data and instructions currently executing. The CPU is able to switch between each set of registers quite rapidly, greatly reducing the cost of context switches.
Only one set of registers is in use at a time, but the host operating system sees two cores instead of the one physical core. Some, if not all, such chips allow a sysadmin to disable the hyper-threading feature, thereby cutting in half the number of apparent cores.
Logical core in essence is physical core * the number of threads that can run on each cores
No. Unless I’ve not kept up with industry developments, hyper-threading creates the illusion of only a second logical core per physical core. So a machine with 12 physical cores with hyper-threading disabled will appear to have 24 cores after enabling hyper-threading.
Not all chips have hyper-threading technology. For example, Apple Silicon chips M1, M2, and M3 in modern Macs do not.
Java has a Request per Thread model
I do not know what you mean. Java does not have “requests”. Edit your Question to clarify.
If you meant Web server requests, that is up to the particular web server implementation. The programmers for a basic web server might choose to launch a thread per request. But platform threads are quite expensive in terms of CPU and memory. So a high-performance web server would instead choose to use one fresh new virtual thread per request, or create their own technology for juggling multiple requests.
Hence if the cpu has 8 logical core, jvm can run 8 process parallelly (not concurrent!)
In theory, yes, a machine with 4 physical cores and 8 logical cores can be running 8 threads (not processes) for a JVM at a time but not literally simultaneously because only half the logical cores are ever actually executing.
But keep in mind that dozens, or likely hundreds, of other processes are running threads too. So the CPU is unlikely to be using all its cores for the JVM for anything more than brief moments.
If a machine were often heavily loaded with processes running CPU-bound tasks, then the tiny amount of context-switching overhead cost in hyper-threading may not be so tiny in total. In this scenario, a sysadmin may consider disabling hyper-threading.
Concurrency refers to a
Be aware that the terms multi-threading, parallel computing, concurrent computing, and concurrency are often used loosely and interchanged.
When in doubt, consult Wikipedia, define your own meaning, and ask an author/speaker to clarify theirs.
Upvotes: 4