Java ExecutorService - Part 2 - Type of Pools

Поділитися
Вставка
  • Опубліковано 17 вер 2024
  • Learn how to parallelize your tasks and operations easily in Java without cooking up your own Threads.
    Part 1: Introduction - • Java ExecutorService -...
    Part 2: Type of Pools - • Java ExecutorService -...
    Part 3: Constructor / LifeCycle - • Java ExecutorService -...
    Part 4: Callable/Future - • Java ExecutorService -...
    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...

КОМЕНТАРІ • 144

  • @abhijitrajan959
    @abhijitrajan959 4 роки тому +21

    Whats amazing is that you make even complex concept interesting and easy to understand :)

  • @thecosmicantinatalist
    @thecosmicantinatalist 5 років тому +134

    That crow needs a task.

  • @mustafakamalsirajudeen8133
    @mustafakamalsirajudeen8133 6 років тому +20

    Excellent. You are touching the complicated areas to understand, but explaining in simple manner. Appreciate.

    • @DefogTech
      @DefogTech  6 років тому +4

      Thanks much! I'm very happy and motivated that's it helping folks.

  • @aftabshaikh200
    @aftabshaikh200 Рік тому +2

    Explanation was very good. I have one opinion, I might be wrong here but I don't think SingleThreadedExecutor executes task in sequential manner. It might pick up the tasks in order but it doesn't mean that it will pick it up after completing the first one. Because, in case first thread is doing some IO operation, the CPU and the thread will start processing the next task.

    • @pramalvi
      @pramalvi 7 місяців тому

      You are right. If one of the I/O task is waiting then single thread can continue to execute other tasks while waiting for the I/O task to complete. FIFO ordering is not guaranteed unless all tasks are CPU based.

  • @somasundaramvalliappan3851
    @somasundaramvalliappan3851 4 роки тому +7

    the videos are amazing, the most interesting aspect is you connect cpu cores caches and the threads, hardware to software, great sir, please continue this you are really a guru thanks👍

  • @adityanehra
    @adityanehra 5 років тому +38

    Explanation is very good Sir. Is that a crow in background !!! :)

    • @DefogTech
      @DefogTech  5 років тому +12

      Thank you! Yes, unfortunately it is a crow.. I wasnt paying attention that day... Subsequent videos have better audio :)

    • @afeesps2299
      @afeesps2299 5 років тому

      lol !!

  • @msteja
    @msteja 2 роки тому +1

    Best concurrency/multithreaded insights playlist ever found!!

  • @sathyendrana3880
    @sathyendrana3880 6 років тому +5

    Explanation is very clear. I have understood the Java threads.

  • @mostinho7
    @mostinho7 3 роки тому +1

    Done thanks
    Types of thread pools
    1. Fixed thread pool
    2. Cached thread pool
    - dynamically creates and terminates threads based on the number of tasks being submitted and wether there are idle threads (frequency of tasks being submitted)
    3. Scheduled thread pool
    - uses a delay queue to keep the tasks, the tasks are not kept sequentially based on when they are submitted, but instead based on when they need to run
    ScheduledExecutorService
    .schedule(...) for running one after delay
    .scheduleAtFixedRate(...) running every x seconds
    .scheduleWithFixedDelay(...) schedule and wait x seconds after task completion
    4. Single threaded executor
    - to ensure tasks are run in order

  • @dilipsutradhar3673
    @dilipsutradhar3673 6 років тому +11

    highly detailed sessions great work.

  • @shekhar_2014
    @shekhar_2014 3 роки тому +1

    This is what I call as an Excellent Explanation .. HatsOff !!! 🤟

  • @sumitbabel5415
    @sumitbabel5415 3 роки тому +1

    Honestly you are exceptionally awesome, such an easy explanation style. Hats off

  • @mahendharkhedhar7887
    @mahendharkhedhar7887 6 років тому +11

    wow awesome explanation

  • @proplayeriguess
    @proplayeriguess 4 роки тому +3

    Excellent explanation Deepak - request you to continue creating more and more videos covering all the major aspects like Collections - DS etc, Great work keep it up :) And thanks a ton !!

  • @mayurdhande8712
    @mayurdhande8712 5 років тому +2

    I really liked the status bar at the bottom of your video.

  • @chinmayasdora2126
    @chinmayasdora2126 5 років тому

    Great explanation.. simple and easy to grab and a quick bite to get a very holistic view about the executors framework. I have watched this and some other videos of this series and are really good. Just to add another difference b/w a "fixed-size thread pool" and a "single-threaded thread pool" is that in case of a "fixed-size thread pool", you can still update/re-configure the pool size set in the constructor later on using the setMaximumPoolSize() method of the ThreadPoolExecutor class.

  • @jingli2168
    @jingli2168 4 роки тому

    I think one thing need to clarify is required between scheduleAtFixedRate and scheduleWithFixedDelay
    1) The fixedRate run periodically, even though the last same execution is not finished, the pool will arrange another thread to proceed, so in this case there are two threads execute the same operation.
    2) The fixedDelay will wait the last same operation to finish and then delay say 10 seconds to restart the same operation.

  • @jaleelpasha3301
    @jaleelpasha3301 7 місяців тому +1

    Introduction:
    - The video discusses the different types of thread pools provided by Java's `ExecutorService`.
    Thread Pool Types:
    1. Fixed Thread Pool:
    - Has a fixed number of threads.
    - All submitted tasks are stored in a blocking queue.
    - Threads fetch and execute tasks sequentially.
    - Ideal for CPU-intensive tasks.
    2. Cached Thread Pool:
    - Does not have a fixed number of threads.
    - Uses a synchronous queue to hold tasks.
    - Creates threads as needed and can kill idle threads.
    - Suitable for IO-intensive tasks.
    3. Scheduled Thread Pool:
    - Used for scheduling tasks to run after a certain delay or at fixed intervals.
    - Tasks are stored in a delay queue.
    - Provides methods for scheduling tasks once, at fixed rates, or with fixed delays.
    4. Single Threaded Executor:
    - Similar to fixed thread pool but with only one thread.
    - Ensures tasks are executed sequentially.
    - Useful for tasks that require strict sequential execution.
    Summary:
    - Java's `ExecutorService` provides various types of thread pools for managing tasks efficiently.
    - The choice of thread pool depends on the nature of tasks, such as CPU-intensive or IO-intensive, and the scheduling requirements.
    - Understanding the characteristics of each thread pool helps in selecting the appropriate one for different scenarios.

  • @aditydud
    @aditydud 4 роки тому

    Complicated theory but you explained it very nicely and now it seems easy to implement.Thanks a ton

  • @vivekdarji6440
    @vivekdarji6440 4 роки тому

    Nice Explanation of critical functionalities in simple way. Thank you !

  • @sadabhasan8218
    @sadabhasan8218 5 років тому +1

    effective and sort video, sir you are dammed good, thanks and good blase you.

    • @DefogTech
      @DefogTech  5 років тому

      Thank you very much!

  • @e.ch.vidyasagarkorada8937
    @e.ch.vidyasagarkorada8937 4 роки тому

    Thank you for tutorial it's very helpful to understand in detail
    Please explain about
    Threadpoolexeuter constructor
    Min
    Max
    Blookingqueue
    Exception
    Up to my knowledge
    Thanks in advance
    Expecting soon

  • @avinashgarg7370
    @avinashgarg7370 2 роки тому

    God level explanation! Thank you.

  • @rewariaditya
    @rewariaditya 4 роки тому +1

    Crows.. sorry .. Bows ..for the explanation :)

  • @alokdeshpande5517
    @alokdeshpande5517 5 років тому

    Crisp clear and precise 👍👍👍

  • @navendugupta2765
    @navendugupta2765 4 роки тому

    Thank you for the video. It solved all my doubts.

  • @johncerpa3782
    @johncerpa3782 3 роки тому

    Thank you for making this series of videos

  • @lzhu12
    @lzhu12 6 років тому +3

    Thank you very much.

  • @vikram85228522
    @vikram85228522 4 роки тому +1

    What is diff with fixthread pool with size 1 and single threaded executors

    • @DefogTech
      @DefogTech  4 роки тому +1

      No difference.. they are the same.

  • @SBala-xk6lr
    @SBala-xk6lr 4 роки тому

    Great vid ! Clean , clear & crisp :)

  • @johnjordandionisio9402
    @johnjordandionisio9402 3 роки тому

    This is brilliant. Thank you for this!

  • @GydenKnight
    @GydenKnight 2 роки тому

    Great video, that bird in the background is making a lot of noise, looks like it also want to learn what you are teaching 🤣😁

  • @rabiunislam9302
    @rabiunislam9302 3 роки тому

    Very clear explanation. Keep it up.

  • @pankajpandey2882
    @pankajpandey2882 3 роки тому

    Your narration is very good 👍

  • @suman6327
    @suman6327 4 роки тому

    Really thanks for putting up such high quality stuff. Coming to ScheduledThreadPool, is DelayQueue actually PriorityQueue - prioritizing the tasks based on execution time ??

    • @DefogTech
      @DefogTech  4 роки тому +1

      no the implementation is different for both, so its a distinct data structure

  • @rajdipbhattacharya1905
    @rajdipbhattacharya1905 3 роки тому

    brilliant dude...keep the good work going

  • @krissling4497
    @krissling4497 4 місяці тому

    great content brother

  • @hueynguyen
    @hueynguyen 3 роки тому

    Amazing explanation! I love ur videos.

  • @koushikdeb7874
    @koushikdeb7874 3 роки тому

    Great work .. but I think the crow was pointing out one mistake its "scheduleWithFixedDelay" not "scheduleAtFixedDelay" as shown in slide

  • @rohitsolanki6421
    @rohitsolanki6421 2 роки тому

    Clear and concise explanation!However,I have some doubts.What is the difference between a Singlethreaded pool and a Fixedpool with single thread? Also does SingleThreaded pool guarantee the execution of tasks in FIFO order?

    • @DefogTech
      @DefogTech  2 роки тому

      Both Singlethreaded pool and a Fixedpool with single thread are the same. Internally SingleThreadPool used normal FixedSizedPool.
      Yes, thats correct, SingleThreadedPool will ensure FIFO order of execution.

  • @ravikumarmanepalli7456
    @ravikumarmanepalli7456 5 років тому +1

    Excellent

  • @crazycom7819
    @crazycom7819 Рік тому

    Nice explanation

  • @naragoud428
    @naragoud428 4 роки тому

    Explained very well .thanks

  • @simonbaker9909
    @simonbaker9909 4 роки тому +1

    Thank you very much for this clear explanation. I would like a little more detail on "scheduleAtFixedRate" method to clarify that if the run time of the task exceeds the rate period, is that an error or does the task simply start immediately again (I assume the latter).

    • @DefogTech
      @DefogTech  4 роки тому +1

      FixedRate scheduler doesn't check finish times of the job, it will keep triggering new ones. The fixedDelay one will wait Post a job's completion before triggering next one

    • @simonbaker9909
      @simonbaker9909 4 роки тому

      @@DefogTech Does triggering mean a new thread, or just that it is ready to go immediately when other run finishes?

    • @DefogTech
      @DefogTech  4 роки тому +1

      @@simonbaker9909 Depends on the threadpool size. If size is greater than 1, it will assign a new thread. So both tasks will overlap (will run in parallel)

    • @suman6327
      @suman6327 4 роки тому

      @@DefogTech Really thanks for taking out time to explain such queries.

  • @rizwansworld
    @rizwansworld 3 роки тому

    Thank you so much for the explanation!

  • @raidtheferry
    @raidtheferry 5 місяців тому

    lmao 2:10 hope u got that glass cleaned up

  • @gavravdhongadi9824
    @gavravdhongadi9824 4 роки тому

    Lucky that I found this channel

  • @adsd7349
    @adsd7349 3 роки тому

    Brilliant

  • @shaliniritti5811
    @shaliniritti5811 Рік тому

    It is just osm!!!!!

  • @rishikumar2800
    @rishikumar2800 4 роки тому

    Hi. Great explanation. Went directly into mind.
    One query. There should be some way to stop the given tasks like for ScheduledatFixedRate right. If i want to exexute a task with rate of 10 and it should end after 50 executions.

  • @bvinodmca
    @bvinodmca Рік тому

    very nice, thanks so much

  • @duckfine6837
    @duckfine6837 3 роки тому

    Pretty good video, well explained

  • @AmandeepSingh-sx9ke
    @AmandeepSingh-sx9ke 5 років тому

    Good work!!!
    Please make more videos on critical topics that could help us to code better
    Thank you 😊🙏🏻😇

  • @vaibhavsawant20
    @vaibhavsawant20 6 років тому +1

    Very nice

  • @ayushjain7555
    @ayushjain7555 9 місяців тому

    One question in case of newSingleThreadExecutor, there will no multithreading right?

  • @ankittikna123
    @ankittikna123 2 роки тому

    Hi
    For a single threaded executor,do we have another advantage than actually running sequential task on a different core ,since such scenario can be achieved sequentially on the main thread as well
    I have understood it to be helping in a non blocking sequential execution of ordered tasks only

  • @rahulgoti3864
    @rahulgoti3864 6 років тому +1

    Awesome!! Thank You

  • @manishmanghwani731
    @manishmanghwani731 3 роки тому

    Thank you for detailed explanation. Although its 3 years old video, but if you get some time, kindly review: Is Delay Queue same as priority queue? Are there in retry options for Single Threaded Executor Service. Suppose the task 1 keeps on failing and we want to retry it just for n number of times and after that Task 2 should be tried.

  • @vijaymishra4232
    @vijaymishra4232 4 роки тому

    very nice explanation

  • @TravelWithRabindra
    @TravelWithRabindra 5 років тому

    Nice explanation. Thank you.

  • @shwetatiwari5265
    @shwetatiwari5265 3 роки тому

    What is a better idea - having multiple SingleThreadPools or multiple threads ?

  • @dineshchandgr
    @dineshchandgr 3 роки тому

    hi. which thread pool we configure? Is it tomcat server threads or executor thread pool?

  • @rajsekharmahapatro
    @rajsekharmahapatro 3 роки тому

    What is the difference between a fixedpool of size 1 and a singlethread pool? Technically are they both same?

  • @ShaunYCheng
    @ShaunYCheng 6 років тому +1

    good explanation thanks

  • @pranavprabhu698
    @pranavprabhu698 3 роки тому

    Can you help me with question.
    Create 15 fix threads that will initialise arraylist with 1000 integer.

  • @deeproy7292
    @deeproy7292 3 роки тому

    not sure where to use single Threaded executor. apparently its better to use a loop for the sequential task execution scenario

  • @nitinkumar154
    @nitinkumar154 Рік тому

    Hello Sir, Can you please explain where we are using the blocking queue here for task submission? or is it internally from java that the submitted task will be stored in the blocking queue and will be executed by the scheduler one by one?

    • @DefogTech
      @DefogTech  Рік тому

      For standard executors the blocking queue is managed internally. For custom executors we can create one and supply it from outside

  • @rohitkishore12
    @rohitkishore12 3 роки тому

    So singleThreadExecuter is sequential execution without any parallelism right?

  • @Shubham-zm9xj
    @Shubham-zm9xj Рік тому

    Thanks

  • @hemanandn
    @hemanandn 3 роки тому

    Hi, can you also create and post videos covering data structures and big (O) notations please.

  • @vardhamankothari3450
    @vardhamankothari3450 4 роки тому

    Was singlethreadedexexutor really required I mean I don't see what benefit it provides over traditional method ?

  • @VishalGupta-vt9bz
    @VishalGupta-vt9bz 4 роки тому

    Hi Sir..
    For newCachedThreadPool you have told theoretically it will create number of thread max upto number of task.
    But I have check it will exceeds.

  • @shobhitmittal77
    @shobhitmittal77 4 роки тому

    Thank you for the clear explanation.
    I have a doubt - isn't the type 4 - "Single threaded executor" same as single threaded application. After all, everything will run in sequence..

    • @DefogTech
      @DefogTech  4 роки тому +1

      yes, but you could have multiple pools in your application right? some can be multi-threaded some can be single-threaded. So at an application you still have many threads running concurrently.

    • @shobhitmittal7787
      @shobhitmittal7787 4 роки тому

      @@DefogTech oh right, Thanks..

    • @monakumari6521
      @monakumari6521 4 роки тому

      @@DefogTech i am still confused that what is the need for single thread executor? Can you please give some specific example.

  • @taufikpirjade5983
    @taufikpirjade5983 4 роки тому

    You are amazing !

  • @fatihmehmetkose
    @fatihmehmetkose 2 роки тому

    Thanks!

  • @TheNeethz
    @TheNeethz 5 років тому

    What are some of the use cases in which cached thread pool is used?

  • @amitgupta7228
    @amitgupta7228 4 роки тому

    Hi could you please provide the video on Transactions Management with complete example and scenarios

  • @mritunjayyadav3788
    @mritunjayyadav3788 4 роки тому

    really like your videos , but I have one question , in case of Single Threaded Executor why thread pool is using blocked queue , if there is only one thread a normal queue will work the same way right ?

  • @AnupSingh154
    @AnupSingh154 3 роки тому

    I am facing an issue, my main program is not terminating after completing the execution, why? My program is same as above.

  • @nagendrasai3946
    @nagendrasai3946 5 років тому

    @DefogTech Just a doubt!. Here in tha case of SingleThreadedExecutor, since only one Thread is there, no need of BlockingQueue right? normal Queue is sufficient.

    • @DefogTech
      @DefogTech  5 років тому

      Still need blocking queue, even though there is only 1 queue taking elements, there are 1 or more threads adding elements from outside to ExecutorService.. so it still requires thread safe queue

    • @nagendrasai3946
      @nagendrasai3946 5 років тому

      @@DefogTech Thank You for making such great videos on Threading.

  • @arpitkashyap1
    @arpitkashyap1 5 років тому

    How does in the scheduled pool, delay queue submit the task to the actual thread.(It search for free thread/ already every task is assigned to a thread/or something else)?

    • @DefogTech
      @DefogTech  5 років тому

      It's other way around. Threads will keep polling for tasks from delay queue like normal thread pool, but delay queue only shows tasks once their time has expired.

  • @6365bharath
    @6365bharath 4 роки тому

    If you can't control the number of threads used in a cached thread pool how does it figure out the number of threads? Does it assign threads based on the CPU Cores?

    • @DefogTech
      @DefogTech  4 роки тому

      It's based on incoming requests (runnable or callable). It will create a thread for each new request if existing ones are busy.

  • @mahmoudelsonbati8673
    @mahmoudelsonbati8673 4 роки тому

    perfect

  • @AyushSharma-ux4fk
    @AyushSharma-ux4fk 3 роки тому

    I would be great if you could share the slides too.

  • @devdeeds
    @devdeeds Рік тому

    Wow

  • @sumitshelar2883
    @sumitshelar2883 5 років тому

    In case of fixed thread pool what will happen exception occurs or the thread gets interrupted, will it impact the assigned pool size

    • @DefogTech
      @DefogTech  5 років тому

      If for any reason a thread dies, threadpool will replace it with another thread.

  • @tarunkundhiya5196
    @tarunkundhiya5196 5 років тому

    Thanks Defog for such a nice explanation . One question why does SingleThreadedExecutor needs a Blocking Queue ? [since there is only one thread]

    • @DefogTech
      @DefogTech  5 років тому

      Exactly, since it had only one thread, what happens if you submit multiple tasks while it is already using that thread to execute a task. It needs to save the tasks somewhere so that it can pick them up later when threas is free

    • @tarunkundhiya5196
      @tarunkundhiya5196 5 років тому +1

      @@DefogTech A queue would definitely be needed. Actually i meant why i blocking one.

    • @DefogTech
      @DefogTech  5 років тому +2

      @@tarunkundhiya5196 ah, good point!! Its a single generic implementation (same class for SingleThreadedExecutor vs FixedThreadPool), thus the blocking queue. So for pulling items out of queue there is single thread but I am wondering what about other thread which pushes into the queue (and what if there are multiple threads which submit the tasks), how to coordinate between these conflicts. I am not sure if normal queue will work since one thread is adding and other thread is removing from the queue.

  • @nenadg3665
    @nenadg3665 5 років тому

    Does SingleThreadedExecutor has its own constructor or we've to use fixedThreadPool and assign to it just one single thread?

    • @DefogTech
      @DefogTech  5 років тому +1

      Generally we always use static factory methods of Executors class. There are some subtle differences between single-threaded and fixed-threadpool of size 1

    • @nenadg3665
      @nenadg3665 5 років тому

      @@DefogTech Ok, Now I see it. But in Your video there is no code snippet for static factory method for singleThreadedExecutor, only for fixed, cached and scheduled thread pools so I thought there is no such method.

    • @DefogTech
      @DefogTech  5 років тому

      @@nenadg3665 Ah, my bad.

  • @harshpalsingh1145
    @harshpalsingh1145 Рік тому

    What about WorkStealingPool?

  • @puneetja
    @puneetja 5 років тому

    I have a scenario where I need to convert around 50000 images to PNG format and then upload them to Box (using Java API's). Performing it sequentially is taking huge time.
    Here is what I'm thinking:
    I can create two threads. One will generate images and another will upload images. I will also create a list in which I will keep adding the image paths. Once the list has 100 (just an example) items, I will upload them and re-initialize the list to empty. Now if I wait for all 100 images to be uploaded and then start generating next set of images (because I'm re-initializing the list to empty), then there is no use of having multiple threads.
    Should I have two lists? Thread1 will generate the first 100 images. Once done, Thread2 will start will start uploading and at the same time the first thread will start generating another set of images to list 2 and so on..
    I'm not sure if this approach is efficient or will work. Any suggestions/advise?

    • @DefogTech
      @DefogTech  5 років тому

      Is there a limit for box API?
      If not, then just create a thread pool of say 10 threads and submit all tasks - 50000 - simultaneously. Let the threading be handled by executor service.
      To optimize more have 2 thread pools, one of size N where N is number of CPU cores for image generation task which is CPU intensive, and second pool of size 20 or so for image uploads which is IO tasks

    • @puneetja
      @puneetja 5 років тому

      @@DefogTechThanks for your response. There is no limit for Box. So how would I manage generation and upload? I mean I need to wait until image(s) has/have been generate. How would I maintain that?

    • @DefogTech
      @DefogTech  5 років тому

      @@puneetja create 2 pools, when submitting the image generation tasks to CPU-pool, pass them a reference (in constructor) of the IO pool. So once the images are generated, within the task, create a new upload image task, and submit it to IO pool.
      This way you dont have to many any queues or threading or locks.

    • @puneetja
      @puneetja 5 років тому

      @@DefogTech Okay and this would be in batches. For example, once 500 images are generated then start uploading them and side by side start generating next set?

    • @DefogTech
      @DefogTech  5 років тому

      Why batch them? Just submit 50000 tasks. Its not a big number. If thread pool size (CPU count) is 10, it will automatically become batch size of 10. All other tasks will be stored in queue.
      Thats the beauty of it, we dont need to worry about that all.

  • @freemeplzplz
    @freemeplzplz 5 років тому

    What is the use case of a single threaded executor?

    • @DefogTech
      @DefogTech  5 років тому +3

      As a monitoring thread, or when only single core CPU is available, or when your use-case demands items to be processed in same order as they arrive.

  • @afagund
    @afagund 4 роки тому

    My attention was all on the crow at certain point.

  • @vaibhavchhabra800
    @vaibhavchhabra800 3 роки тому +2

    Too many crows in your area :P

  • @Hemantkumar7990
    @Hemantkumar7990 6 років тому

    good video but the crow is very consistent in the background :)

    • @DefogTech
      @DefogTech  6 років тому +1

      Sorry about that. This is my earlier video. These days I take special care to record only when background noise is less.

    • @manishsrms
      @manishsrms 6 років тому

      Thought it (Crow sound) was bit irritating but your explanation was superb. Thanks for sharing your knowledge :)

    • @kzu8976
      @kzu8976 5 років тому

      rofl "consistent"

  • @jainprateek904
    @jainprateek904 Рік тому

    Am I the only one who is hearing crow sound

    • @DefogTech
      @DefogTech  Рік тому

      no you are not the only one, there are lot of comments about the crow :)