Does Java use parallelism when we use Threads automatically? For example, If I've 4 CPU cores, if I create 4 threads, will they be executed equally among the existent CPUs?
Java's standard threads (now called Platform Threads) are OS threads - so it is up to the OS to decide where your threads run. In many cases your threads will run on multiple CPU cores. But if the computer is very busy on all CPU cores with other tasks, your threads might end up running on the same core, or some threads on the same core at least.
@@JakobJenkov thanks for replying teacher! I recently saw your channel and it’s being grate to see all videos. They are quite well explained and detailed!
great content! any chance to do something similar for the CompletableFuture api? there are many tutorials but none of them are in depth or show practical, real life examples
Thanks for the amazing video :) I have a question for concurrency. If I have for example 8 CPUs you said that, I can create thread pool with 8 threads and execute tasks in the same time. But what about others programs running on the same machine? Whats their impact? Our JVM is not only thing, we have operating system etc.
That is true - then JVM is often not the only process running on the machine. To be sure you would have to measure performance with different number of threads.
is that Thread concept is interduce by you in java 😉? The why you are explaining like you are the founder of thread its really very clear thank you so much for this video's
Thank you !! 😁 ... No, I am not the founder of threads in Java... 😁 ... but I have been explaining Java related concepts since 2006, so I had a but of practice 😊
In case of cancelling it, do we know where it will be stopped? I mean, in previous videos from this exceptional series of multithreading and parallelism, it was sometimes we need to add a flag like "wasCancelled" to stop it in a specific point where it's "safe" for our case. Should we do the same for Future and Callable?
As far as I remember, if a task is started - it will finish. I cannot remember if the Java ExecutorService sends a cancel signal to the task. But if a task has been queued in the ExecutorService but not yet started, it will not be started at all.
I have planned to do so 😊 Until then, you can start with the textual version of my ForkAndJoinPool tutorial, here jenkov.com/tutorials/java-util-concurrent/java-fork-and-join-forkjoinpool.html
The documentation says that ExecutorService#shutdown() does not wait for previously submitted tasks to complete execution docs.oracle.com/javase/7/docs/api/java/util/concurrent/ExecutorService.html#shutdown()
The method shutdown() does not wait for the ExecutorService to fully shut down - before exiting the shutdown() method. However, the ExecutorService will not be fully shutdown until all submitted tasks have been executed. In other words, the shutdown process continues asynchronously. To wait for all these tasks to finish executing, use awaitTermination(). I apologize if the video is a bit unclear here.
@@JakobJenkov thanks for the great content and crystal clear expectations. I just want to be sure that I understood the comment here correctly, so, does the shutdown method block the main thread until all tasks have completed, or it waits for all tasks to be completed and then shutdown the executor service without blocking the main thread ? Thanks in advance
shutdown() itself does not block the main thread, but you can use awaitTermination() if you want to block and wait for the tasks to complete and the ExecutorService to shut down.@@fadidasus6329