Java Concurrency Interview - What is an Ideal Threadpool size?

Answer - the size depends on number of CPU cores and whether the task is CPU bound or IO bound.
For CPU bound (CPU intensive) tasks, the ideal size is same as number of CPU cores.
For IO bound tasks, ideal size depends on the wait time of the IO task. More the wait time, more number of threads can be used to ensure maximum CPU utilization.
Channel
----------------------------------
Complex concepts explained in short & simple manner. Topics include Java Concurrency, Spring Boot, Microservices, Distributed Systems etc. Feel free to ask any doubts in the comments. Also happy to take requests for new videos.
Subscribe or explore the channel - / defogtech
New video added every weekend.
Popular Videos
----------------------------------
What is an API Gateway - • What is an API Gateway?
Executor Service - • Java ExecutorService -...
Introduction to CompletableFuture - • Introduction to Comple...
Java Memory Model in 10 minutes - • Java Memory Model in 1...
Volatile vs Atomic - • Using volatile vs Atom...
What is Spring Webflux - • What is Spring Webflux...
Java Concurrency Interview question - • Java Concurrency Inter...

Пікірлер: 122

  • @adame26
    @adame263 жыл бұрын

    It's been a while since your last video! please share your knowledge again, you have a very good way of explanation. you might as well do udemy courses or create a paid courses on your youtube channel

  • @dishithpoojary
    @dishithpoojary3 жыл бұрын

    Hey the channel is amazing! Just wanted to know when will you start uploading new videos?

  • @sayyedyasin7646
    @sayyedyasin76463 жыл бұрын

    Hello Defog Tech Thanks for the clear and candid explanation. PFB, one of the formulae for ideal number of threads. #of Threads

  • @subbu6647
    @subbu66474 жыл бұрын

    Simple explanation ! Golden stuff! Thanks

  • @peeyar2000
    @peeyar20004 жыл бұрын

    Wow... A very complex topic in a very simple way...Love you man.

  • @prateekkanujiya9775
    @prateekkanujiya97754 жыл бұрын

    This word would not enough but please take it as appreciation far beyond : AWESOME

  • @GauravSingh-ov9mh
    @GauravSingh-ov9mh3 жыл бұрын

    The best ever video on thread. Looking forward to more videos. 👍

  • @shachindrapandit6040
    @shachindrapandit60404 жыл бұрын

    what an explanation brother, really good, clear and very helpful .. thanks a ton!!

  • @dmitrikonnov922
    @dmitrikonnov9223 жыл бұрын

    Your explanations are amazing. I believe, you ARE the gift given me for all the efforts I made before by learning Java and Spring :D . Thank you a lot!

  • @zackzhu9865
    @zackzhu9865 Жыл бұрын

    This video has the clearest explanations around this topic from what I have ever watched!

  • @tombert512
    @tombert5123 жыл бұрын

    These really are excellent videos. I've been a software engineer for almost a decade now, and while I more or less understand concurrency theory for distributed systems (well, as much as anyone CAN understand it :) ), I will admit that I never really bothered learning much about low-to-mid-level multithreading. These videos are easy to follow, well presented, and provide a lot of "bang for your buck" time-wise.

  • @ArjunSingh-hr8iz
    @ArjunSingh-hr8iz4 жыл бұрын

    one of the beat video tutorial in short time in easy way

  • @nareshnegi7535
    @nareshnegi75352 жыл бұрын

    best explanation i have come across so far on threadpool and its dilema. I spent last one week reading windows via c trying to understand what you explained here in under 15 mins. Thanks a lot.

  • @yadegaSabre
    @yadegaSabre4 жыл бұрын

    Thank very good explained!

  • @VikiSangani
    @VikiSangani4 жыл бұрын

    Very nice explanation, pls keep sharing such exclusive knowledge. Thank you.. Awaiting for more such videos. Thanks once again. 👍

  • @tom_see
    @tom_see2 жыл бұрын

    amazing, this is such a good explanation, very simple and clear. No wasted time. Thank you!!!

  • @sudeepsinha9379
    @sudeepsinha93793 жыл бұрын

    Amazingly explained, wonderful job

  • @DipannitaShil
    @DipannitaShil3 жыл бұрын

    All your videos are very informative and insightful. Wish you'd make more such videos!

  • @kamakhyadas6121
    @kamakhyadas61213 жыл бұрын

    Very well presented and explained. Keep up the good work!

  • @user-oy4kf5wr8l
    @user-oy4kf5wr8l4 жыл бұрын

    All my respect Sir! Plz continue :)

  • @dataguy7013
    @dataguy70132 жыл бұрын

    Wow, best explanation ever, Thank you

  • @gurusravankallubhavi3714
    @gurusravankallubhavi37143 жыл бұрын

    You are a gifted teacher!

  • @divyangshah99
    @divyangshah994 жыл бұрын

    Awesome video!!! Enlightening

  • @mysavingclub
    @mysavingclub3 жыл бұрын

    excellent video. I was searching from long time

  • @creatingwaves
    @creatingwaves3 жыл бұрын

    Amzing content. Provides great clarity. Please do a series on the design patterns. And object oriented design like designing a parking lot.

  • @fakruu
    @fakruu2 жыл бұрын

    Please continue posting videos. Not everyone can explain complex concepts in a simple manner.

  • @ssbahubali1055
    @ssbahubali10554 жыл бұрын

    Perfect !!!

  • @shellindebted5328
    @shellindebted53284 жыл бұрын

    Sir, that's another Masterpiece from you...Thanks !

  • @dineshchandgr
    @dineshchandgr3 жыл бұрын

    Wow! Amazing explanation.

  • @Dipubuet
    @Dipubuet Жыл бұрын

    Really really helpful video!!!

  • @solorankerone
    @solorankerone4 жыл бұрын

    Thank you very much for the video 😁

  • @dipenmavani4404
    @dipenmavani44042 жыл бұрын

    Great explanatory video 👍👍

  • @kumarc4853
    @kumarc4853Ай бұрын

    learned a lot, useful to tlak about these things for any lowlevel design discussion, staff eng level experience gained!!!

  • @algorithmimplementer415
    @algorithmimplementer4154 жыл бұрын

    Excellent - so much relevant content in less than 15 mins.

  • @akashshirale1927

    @akashshirale1927

    3 жыл бұрын

    u r that famous guy on leetcode

  • @ilashree6235
    @ilashree62353 жыл бұрын

    Wow, thanks a lot for making such informative videos

  • @sripolisettys
    @sripolisettys3 жыл бұрын

    Perfect!!

  • @pdteach
    @pdteach4 жыл бұрын

    Thanks for such a nice explanation

  • @harry12200
    @harry122003 жыл бұрын

    Please start making videos again. Your videos are very informative and easily understandable. Hope to see you soon making videos.

  • @KunalKishoreSharma
    @KunalKishoreSharma3 жыл бұрын

    Nicely explained, thanks for the video 👍👍

  • @linkin324
    @linkin3242 жыл бұрын

    Excellent explanation, loved the nuances and detailing

  • @VikasSharma-io5ip
    @VikasSharma-io5ip3 жыл бұрын

    Tremendously awsum explanation.

  • @bahaagamal4995
    @bahaagamal49954 жыл бұрын

    perfect ...thank you

  • @anurag9110
    @anurag91103 жыл бұрын

    Great explanation

  • @mildtime8146
    @mildtime81463 жыл бұрын

    Really nice explanation !!

  • @mostinho7
    @mostinho73 жыл бұрын

    Done thanks CPU bound tasks (no blocking) pool size is number of cores 7:30 IO bound task diagram 11:30 Formula for number of cores wait time is how much time the thread is in wait state, and cpu time is how much time the thread spends executing For example, if the task executes for 1 second and 0.5 seconds of that is blocked on IO, then ideal thread count will be if we have 1 core 1(1+ (0.5/0.5)) = 2 threads What if the task blocks longer? Say total execution time 1 second but 0.8 blocked Ideal thread count = 1*(1+(0.8/0.2)) = 5

  • @reductor_
    @reductor_ Жыл бұрын

    13:31 "L1/L2 etc need to be flushed during a thread switch", while it's architecture specific this is only needed for switching between processes, however if switching between different threads in the same process they share the same address space, so no flushing is needed (ignoring relevant fences). Also it the L2 cache is shared between multiple cores and wouldn't be flushed however the TLB and L1 potentially are.

  • @avinashsakroji1811
    @avinashsakroji18113 жыл бұрын

    Nicely explained

  • @ramkumar_balu
    @ramkumar_balu3 жыл бұрын

    Pls continue uploading new videos.. The channel is idle, you should continue.. it is really amazing..

  • @rizwanfirdous
    @rizwanfirdous4 жыл бұрын

    Excellent 👌 explanation. Please do more videos on java concurrency. Also my suggestion is it would be great if you create videos which covers java concurrency book with your animation and explanation Thanks again

  • @ranawareviraj
    @ranawareviraj4 ай бұрын

    Now I understand it :)

  • @asthiwanka
    @asthiwanka4 жыл бұрын

    One of the best explanations for thread pooling I have seen so far which many have failed to do. It would be great if you can extend the discussion on how hyperthreading works along with thread pools.

  • @DefogTech

    @DefogTech

    4 жыл бұрын

    Good idea... I want to explore hyperthreading concept, might do separate video on it

  • @SuccessToSamadhi
    @SuccessToSamadhi Жыл бұрын

    Thank you

  • @RahulSharma-ir6bg
    @RahulSharma-ir6bg2 жыл бұрын

    Your videos are short and sweet. If you have some time to spare with us, I expect rx Java from you. Thanks.

  • @anthonya880
    @anthonya8802 жыл бұрын

    You are one of the best tech youtubers. Please come back to youtube.

  • @johnathanrice3569
    @johnathanrice35699 ай бұрын

    This advice is true if it assumes that there are no other threads running on the computer computer but of course there are thousands of threads running typically or at least hundred and so the more threads you allocate the more the scheduler gives time to your application. In addition to increasing the nice number / priority

  • @DefogTech

    @DefogTech

    9 ай бұрын

    Agreed. Though its more true during development and testing where you application is running with many others. For production though, even with containers typically the application has fixed set if resources assigned and the sharing is minimal

  • @kelvinpage4647
    @kelvinpage46474 жыл бұрын

    Impressive

  • @AdilTheHAckerUnknown
    @AdilTheHAckerUnknown4 жыл бұрын

    We would love to have live Q&A if possible

  • @TKNDevCorner
    @TKNDevCorner4 жыл бұрын

    Hi Defog Tech , thanks for the tutorials ! Could you make videos (even better if a playlist of tutorials) about advanced topics and real-life examples about Maven ? For example, releasing of projects with complex versioning, git/jenkins/artifactory integration etc... Any intermediate - advanced topics would be very much appreciated. Online tutorials nowadays about maven only cover very basic concepts and they are all the same (e.g. dependencies ... everyone knows about it).

  • @SreekarAnugu
    @SreekarAnugu3 жыл бұрын

    For people who don't have patience: The answer is: it depends on whether the task is CPU intensive or IO intesive. If it's CPU intensive, it is = number of cores, if it's IO bound, it is = depends on how much time the IO intensive task is eating on CPU. Usually much more than the number of cores.

  • @angNguyen-bc8ge
    @angNguyen-bc8ge2 жыл бұрын

    Waiting for new videos from your channel

  • @9467889975
    @94678899753 жыл бұрын

    Awesome explanation, just a view you may add memory usage of the task as an important parameter in the formula because it is one of the essential factor which restrict us in number of threads. Also you may add some explanation like what will happen if you try to start 1000 threads but fixed number of parallel threads are only 10. Mean will CPU keep them in memory or what would be the scenario. Other than this i felt its a very good explanation. :)

  • @dimasnaresh8268
    @dimasnaresh82684 жыл бұрын

    Sir, could you please make video about differences between embeded server and standalone server at performances aspect in Java ecosystem ? I love the learning more deep about that and how it takes benefit for microservice architecture

  • @mozifbeigh1838
    @mozifbeigh18382 жыл бұрын

    Thanks for the Video. Just curious about scenario for I/O tasks scenario if the I/O is non-blocking IO(In above Video it is assumed I/O is blocking I/O). It just appears in that case increasing the size of threadpool may not give any benefit as the thread will need not to wait for I/O in non-blocking scenario and can take up another task in meantime and even one thread (in single core machine scenario ) may suffice .

  • @manoranjanm1384
    @manoranjanm13842 жыл бұрын

    Hello Sir, your explanations r very pin point, please upload some Design patterns best practices and application level Exception hanndlings . SSL usage rest security like basic, oath , encryption realtime usage in microservices and normal projects

  • @rahulchandrabhan
    @rahulchandrabhan3 жыл бұрын

    Super video! I applauded for ₹40.00 👏

  • @pulkitgupta8696
    @pulkitgupta86964 жыл бұрын

    Wow...

  • @paprikar
    @paprikar Жыл бұрын

    An alternative formula for the number of threads would also be: N = CORES * (ALL_TIME / CPU_TIME), where ALL_TIME is just IO_TIME + CPU_TIME.

  • @johnathanrice3569

    @johnathanrice3569

    9 ай бұрын

    All time is IO time plus CPU time plus other thread time. The more thread you run the more time you’ll get scheduled for each threads overall. Instead of 1/1000 you’ll get 100/1100 all things else considered equal

  • @sagarshekhar6296
    @sagarshekhar62964 жыл бұрын

    I always got confused when I was asked this question in interviews........Thanks for elaborating it........Excellent explanation...

  • @ramp4409
    @ramp44094 жыл бұрын

    Confused ... but i know you are explanations are good

  • @subhasispanda9000
    @subhasispanda90004 жыл бұрын

    can you make videos how threads contribute to L1/L2/L3 cache memory. give some practical scenarios.. thanks for this nice videos..

  • @TrulyLordOfNothing
    @TrulyLordOfNothing3 жыл бұрын

    I don't know if you realize it but you're content is great. I hope more poeple press the applaud button that supports you monetarily. Please continue making videos.

  • @ashvajitchauhan608
    @ashvajitchauhan6084 жыл бұрын

    Thanks for amazing explanation, pls upload videos for spring

  • @harishkumarrayasam
    @harishkumarrayasam3 жыл бұрын

    Is there an overheard of creating a threadpool rather than using the java system threads using completablefutures?

  • @nunu-ej6to
    @nunu-ej6to2 жыл бұрын

    Best explanation I have heard. But how do I estimate the CPU time and Wait time accurately ?

  • @PA-vf5st
    @PA-vf5st3 жыл бұрын

    Nice Video....Can you pls explain the Thread/Core management in Cloud environment where core size can be in millicores....

  • @AleksandarT10
    @AleksandarT104 жыл бұрын

    Please do more videos

  • @ashvajitchauhan608
    @ashvajitchauhan6084 жыл бұрын

    Please upload more videos

  • @kevinding0218
    @kevinding0218 Жыл бұрын

    Great explanation! What if we have multiple threadpool instead of just one single threadpool in an application, can each still have same coreThreadSize?

  • @LivenLove
    @LivenLove3 жыл бұрын

    Hi I have been following your videos since few years now, and have started reading JCIP book for further clarity. Ny question is, Do programmers of bigdata field too need to understand concurrency?? Or is it handled by the latest frameworks like spark storm etc

  • @DefogTech

    @DefogTech

    3 жыл бұрын

    For BigData processing I dont think understanding of Java concurrency is necessary. You might need to understand concurrency in general at data flow level, but not in-depth like these tutorials.

  • @AleksandarT10
    @AleksandarT102 жыл бұрын

    Where did u go Defog Tech? please come back

  • @sarthakkanungosar
    @sarthakkanungosar4 жыл бұрын

    What is consistent hashing?

  • @karinachavarin7296
    @karinachavarin72962 жыл бұрын

    Why did you stop making videos, these are so helpful..

  • @Rahul-pr1zr
    @Rahul-pr1zr Жыл бұрын

    Great explanation. I had a question - what happens if a method has both IO and CPU bound operations? Does IO bound operation take precedence over CPU bound operation?

  • @DefogTech

    @DefogTech

    Жыл бұрын

    I didnt fully understand. If method has sequential code it will be executed in that sequence irrespective of whether its IO or CPU bound. At a kernel level too, threads are given equal opportunity to run and I dont think IO vs CPU is a scheduling factor.

  • @MrMikomi
    @MrMikomi3 жыл бұрын

    Wondering how to find out the wait time and CPU time 🤔 Maybe jstack can help...not sure

  • @stephyjacob1256
    @stephyjacob12564 жыл бұрын

    @Defog Tech what happened man? you are not posting any more videos .

  • @uka0002
    @uka00024 жыл бұрын

    Wow .. great respect for your way of teaching and knowledge...Do you have any courses in Udemy.. I’ll buy it...

  • @DefogTech

    @DefogTech

    4 жыл бұрын

    No courses as of now Uday, might start KZread memberships if there is enough interest

  • @mandeepsharma8305
    @mandeepsharma83053 жыл бұрын

    I think you should appear sometimes in videos person talk to your students. It will surely help your channel. It's been a year You have not uploaded any videos. I think you have potential to become next java brains. I hope you start uploading as project reactor is maturing and I don't see anybody else who could teach devs write non blocking code. Or you can jump into gRPC. Thank you for for all these efforts. Start coming live turn on the super chats. Join button won't help it is more of commitment and you know commitments.

  • @afifkhaja
    @afifkhaja3 жыл бұрын

    Extremely good explanation. Let's say I have 2 cores and I create a thread pool of size 2. That means I have the main thread plus 2 child threads = 3 threads total, right? Why isn't the main thread being taken into account in your calculations? Thanks

  • @DefogTech

    @DefogTech

    3 жыл бұрын

    AFAIK, in webservers in addition to main-thread there are lot of other threads (eg: GC thread). Have used only request accepting/processing threads in this example to keep it simple.

  • @5amyak
    @5amyak Жыл бұрын

    How to calculate the wait time and CPU time of a task ?

  • @voiceguy554
    @voiceguy5542 жыл бұрын

    It has been a while since we got new tutorial from you. Hope you are doing fine.

  • @sarthakkanungosar
    @sarthakkanungosar4 жыл бұрын

    Can you please add a video for it?

  • @harishsure9562
    @harishsure9562 Жыл бұрын

    writing data into a file is a CPU IO operation right?

  • @sreejiths8159
    @sreejiths81594 жыл бұрын

    Hi, I am very new to the java concurrent programming. Your playlist have helped me a lot in understanding the basics. Thank you so much for giving such a simple explanation. I have a doubt regarding thread count calculation, suppose there is a system which need to get some large number of customer data of from DB/System (ie IO operation) and want to process, validate (ie CPU intense operation) data of each user and update the validation status back to DB/System (ie IO operation). How should I calculate/ design the thread count? Please correct me if I am wrong about the scenario. Thanks in advance.

  • @DefogTech

    @DefogTech

    4 жыл бұрын

    Good question. It depends on how much time it takes for read IO operation to complete which will define rate of incoming tasks and how much time the CPU intensive operation takes which will define how many of these tasks can you run in parallel (at a time). So if CPU intensive task takes 10ms, then you can have max N of such tasks, where N = Number of CPU cores.

  • @sreejiths8159

    @sreejiths8159

    4 жыл бұрын

    @@DefogTech Got it . That make sense.Thank you for the explanation.

  • @vikaskokae
    @vikaskokae3 жыл бұрын

    For IO bound tasks, we can have more threads in the pool for a fixed number of CPU cores. In real life scenarios, have you performed trial and error to arrive at the ideal count of threads, by observing the average wait time for such tasks?

  • @akhileshguptaakhi
    @akhileshguptaakhi4 жыл бұрын

    Sir, what about file operation tasks such as PDF/Excel report creation, is it a CPU intensive or IO-intensive? My guess would be IO intensive

  • @DefogTech

    @DefogTech

    4 жыл бұрын

    creating the format in-memory would be CPU intensive, and post that writing to the disk would be IO

  • @pramodjalindra5414
    @pramodjalindra54143 жыл бұрын

    Let's suppose I have hit the post api and saved the data in db via jpa ... Now I am using back button and resubmitting the request again 3 time and each times data is pushed in db ..and I don't want to insert resubmitted data in the table , could you please tell me how we can achieve this TIA 😊

  • @DefogTech

    @DefogTech

    3 жыл бұрын

    you should use an idempotency key which is hash of the data being inserted, and set unique constraint within DB itself. this should not be handled at code level IMO

  • @pramodjalindra5414

    @pramodjalindra5414

    3 жыл бұрын

    @@DefogTech thanks let me try 😊😊

  • @sarthakkanungosar
    @sarthakkanungosar4 жыл бұрын

    What is database sharding?

  • @DefogTech

    @DefogTech

    4 жыл бұрын

    I have a series coming up about NoSQL which will cover sharding/partitioning

  • @updownftw
    @updownftw Жыл бұрын

    I have one confusion: how many types of thread are there ? I know there are threads related to cpu, then there are probably os threads, and then there are java threads. How do they relate to one another ?

  • @DefogTech

    @DefogTech

    Жыл бұрын

    Behind the scenes everything is a thread of same type. Every process, whether its OS utilities or applications or services, work using threads. I am sure there are commands for every OS which lets you find how many threads each process is using. Try that out for various types of applications. There are no threads related to CPU AFAIK.

  • @updownftw

    @updownftw

    Жыл бұрын

    @@DefogTech Thankyou for your reply Deepak. So when a processor says i9 8 core and 8 threads. That means they are saying 8 things can run parallelly on those 8 cores at at time ?

  • @DefogTech

    @DefogTech

    Жыл бұрын

    @@updownftw yes, correct. 1 thread per core. Note: There is a feature in both Intel and AMD CPUs called SMT which allows 2 threads per core, but even that is not technically concurrent, and its disabled generally due to recent vulnerabilities.

  • @updownftw

    @updownftw

    Жыл бұрын

    @@DefogTech Ok, thanks Deepak 👍🏽

  • @parvezmd6455
    @parvezmd64554 жыл бұрын

    sir,explan the process,thread,task term with real time examplr.confuse alot. thank u

  • @sireesham4208

    @sireesham4208

    4 жыл бұрын

    Process: A process is an instance of a computer program that is being executed. It contains the program code and its current activity. Depending on the operating system (OS), a process may be made up of multiple threads of execution that execute instructions concurrently. Process-based multitasking enables you to run the Java compiler at the same time that you are using a text editor. In employing multiple processes with a single CPU,context switching between various memory context is used. Each process has a complete set of its own variables. Thread: A thread is a basic unit of CPU utilization, consisting of a program counter, a stack, and a set of registers. A thread of execution results from a fork of a computer program into two or more concurrently running tasks. The implementation of threads and processes differs from one operating system to another, but in most cases, a thread is contained inside a process. Multiple threads can exist within the same process and share resources such as memory, while different processes do not share these resources. Example of threads in same process is automatic spell check and automatic saving of a file while writing. Threads are basically processes that run in the same memory context. Threads may share the same data while execution. Thread Diagram i.e. single thread vs multiple threads Task: A task is a set of program instructions that are loaded in memory.

  • @BimalModak
    @BimalModak3 жыл бұрын

    Looks like this is your last uploaded video on this channel. Why you have stopped creating these awesome tech videos? Is everything alright bro?