Java ExecutorService - Part 3 - Constructor & LifeCycle methods

Поделиться
HTML-код
  • Опубликовано: 19 ноя 2024

Комментарии •

  • @pratikmishra4745
    @pratikmishra4745 5 лет назад +68

    ultimate!!! Now let the interviewer ask something about executorservice. GAME ON!!

  • @RohitPal-lz1wf
    @RohitPal-lz1wf 3 года назад +14

    I have enrolled for course on Udemy for multi threading but this is far better than that... Hats off for detailed information. Keep posting thanks

  • @arambh-gaur
    @arambh-gaur Месяц назад

    Your playlist on java concurrency is by far the best out there on youtube

  • @jaleelpasha3301
    @jaleelpasha3301 9 месяцев назад

    Introduction:
    - The video explores advanced topics related to the `ExecutorService` in Java.
    - It covers parameters for creating thread pools, types of queues, rejection handling, and lifecycle methods.
    Advanced Topics Covered:
    1. Parameters for Creating Thread Pools:
    - Core pool size: Initial size for the thread pool.
    - Max pool size: Upper threshold for the thread pool size.
    - Keepalive time: Time for which idle threads remain alive before being killed.
    - Work queue: Type of queue used to store tasks.
    - Thread factory: Factory for creating new threads.
    - Rejection handler: Policy for handling rejected tasks.
    2. Types of Queues:
    - Linked blocking queue: Unbounded queue for fixed and single-threaded executors.
    - Synchronous queue: Queue with a single storage slot for cached thread pool.
    - Delayed work queue: Queue for scheduling tasks based on scheduled time.
    3. Rejection Handling Policies:
    - Abort policy: Throws a runtime exception for rejected tasks.
    - Discard policy: Silently discards rejected tasks.
    - Discard oldest policy: Discards the oldest task in the queue for rejected tasks.
    - Caller runs policy: Asks the caller to execute the rejected task.
    4. Lifecycle Methods:
    - `shutdown()`: Initiates shutdown but does not immediately shut down the executor service.
    - `isShutdown()`: Checks if shutdown has been initiated.
    - `isTerminated()`: Checks if shutdown is completed.
    - `awaitTermination()`: Blocks until all tasks are completed or a timeout occurs.
    - `shutdownNow()`: Initiates shutdown and returns tasks that were not executed.
    Summary:
    - Understanding parameters for creating thread pools helps customize the behavior of the `ExecutorService`.
    - Different types of queues are used based on the nature of the thread pool and task scheduling requirements.
    - Rejection handling policies dictate how rejected tasks are managed.
    - Lifecycle methods help control the shutdown process of the executor service.

  • @RahulSingh-qe4sw
    @RahulSingh-qe4sw 2 года назад +1

    Thanks a lot. Multi-threading concepts are more clear after watching your videos.

  • @narendragadhela760
    @narendragadhela760 9 дней назад

    Want more playlists like this Sir, you made our life easy

  • @Admiral.MacDonnell
    @Admiral.MacDonnell 10 месяцев назад

    You are the real deal! Who is watching in 2024?

  • @kingshukjana4718
    @kingshukjana4718 5 лет назад +8

    The comparative analysis diagram in 4:15 is awesome...!!

  • @viveksinghdroid
    @viveksinghdroid 3 года назад

    Thanks RUclips for recommending this amazing channel

  • @akshatmehra3951
    @akshatmehra3951 2 месяца назад

    Great videos as always, thank you so much for all the learning content here

  • @GoddamnAxl
    @GoddamnAxl 5 лет назад +33

    I miss the crow already :

    • @nishanthk9100
      @nishanthk9100 4 года назад +2

      Let's ask him to put a virtual CROW as background music while he is editing :D

  • @harishgyanani6656
    @harishgyanani6656 3 года назад +1

    Super video! I applauded for ₹40.00 👏

  • @mahendharkhedhar7887
    @mahendharkhedhar7887 6 лет назад

    Really got clarity helped me in interviews,lots of confusion in understanding thread pool u cleared every thing,one more request from my side we want vedios on nio streams,reactive programming sir pls help us by providing u r vedios u r really adorable

    • @DefogTech
      @DefogTech  6 лет назад +1

      I definitely plan for reactive programming videos in coming weeks. I'm glad these videos are helping folks

  • @mostinho7
    @mostinho7 3 года назад +8

    0:30 instantiating the fixed thread pool with newFixedThreadPool calls the ThreadPoolExecutor constructor behind the scenes
    Core pool size initial number of threads in pool
    Current pool size can be bigger if new threads created
    Max pool size
    Keep alive time if thread not active gets killed and pool decreases
    Core threads are not killed when idle except if allowCoreThreadTimeOut is true
    3:00 pool params for different executors
    5:00 queue types for different executors
    LinkedBlockingQueue can keep increasing, used for fixed and single threaded executors, because the threads are bounded, so the task storage area need to be unbounded so we can keep submitting tasks asynchronously
    SynchronousQueue is a queue with a single slot, used in CachedThreadPool because threads are unbounded so storage space for tasks can be bounded, new threads will be created to take the tasks being submitted
    DelayedWorkQueue for scheduledThreadPool
    ArrayBlockingQueue fixed size, if queue is full new thread is created up to maxPoolSize
    Continue notes

  • @amoltulaskar2734
    @amoltulaskar2734 4 года назад

    Just read all parts. Awesome.....

  • @sachinjadhav8759
    @sachinjadhav8759 5 лет назад +2

    Excellent.Nice explanation

  • @kakashihatake4840
    @kakashihatake4840 6 лет назад +6

    Thank you so much. Very informative

  • @sagarshekhar6296
    @sagarshekhar6296 6 лет назад +1

    All the videos are awesome and very deeply explained......Thanks a lot

    • @DefogTech
      @DefogTech  6 лет назад

      You're welcome sir! Thank you for the kind words.

  • @sahilgandhi9156
    @sahilgandhi9156 Год назад

    Superb explanation, Thanks

  • @MrWicky77
    @MrWicky77 6 лет назад +2

    Good explanation and really good content. Please make video tutorial on JAVAFX Concurrency if possible. Good going keep it up.

    • @DefogTech
      @DefogTech  6 лет назад +1

      Thank you. I am not familiar with JavaFX.. I am planning on more videos of Spring Boot, Cloud Foundry and Kotlin. Let me know if you want any other topics.

  • @dheebanm3207
    @dheebanm3207 Год назад

    Extraordinary video sir

  • @balajibalu3240
    @balajibalu3240 6 лет назад +1

    Very well explained..understood the concept ..please upload videos on restful web services

    • @DefogTech
      @DefogTech  6 лет назад

      Thank you! Will try for web services

  • @rohitvlogscanada
    @rohitvlogscanada 6 лет назад +4

    unique videos bro

  • @rajesh.singha
    @rajesh.singha 4 месяца назад

    Hats off. This is awesome

  • @abhik9450
    @abhik9450 5 лет назад +2

    Amazing explanation, thank you

  • @GanaviN-f1o
    @GanaviN-f1o 4 месяца назад

    well explained. thanks!

  • @deepmingle
    @deepmingle 4 года назад

    Awesome job .. so thoroughly explained .. just loved it 🙌 👏 👍

  • @KosalaSubasinghe1
    @KosalaSubasinghe1 4 года назад +1

    Amazing explanation. thanks a lot.

  • @mahendratake2397
    @mahendratake2397 3 года назад

    Simply Awesome Bruh !!!!!!!!!!!!

  • @narendragc9582
    @narendragc9582 4 года назад

    Really awesome bro.keep doing more

  • @shaocongdong2795
    @shaocongdong2795 3 года назад

    Well explained. Thank you, sir.

  • @shobhitmittal77
    @shobhitmittal77 4 года назад +1

    LifeCycle methods begin at 10.25

  • @imkrrishnayak
    @imkrrishnayak 5 лет назад +1

    very good explanation!!!

  • @sonaljambhule6510
    @sonaljambhule6510 6 лет назад +1

    Very good explaination bro.....thnx

    • @DefogTech
      @DefogTech  6 лет назад

      You're welcome! Thank you for the kind words.

  • @nitantjoshi8614
    @nitantjoshi8614 4 года назад

    Wow, amazing video. Just osm man

  • @npchidgopkar
    @npchidgopkar Год назад

    Is the PPT/notes available for quick revision. If yes please can u share it.

  • @avinashvutukuri652
    @avinashvutukuri652 4 года назад

    Great videos, very helpful.
    I have a question regarding the shutdown aspect of the ExecutorService. The whole idea of the ThreadPool is to reuse the threads without creating new Threads time and again as it is a very expensive operation. Then when do we actually opt for shutdown and recreation of a ExecutorService. Many online tutorials and blogs I have seen, the pattern followed is
    In a method these sequence of steps are performed -- create an executorservice with fixed threadpool ---> executor.submit all tasks --> executor.shutdown() --> Read the future result when ready (Which is a blocking call).
    So in this model whenever the method of main thread gets executed, the above sequence of steps are repeated where in we are creating and shutting down the threads repeatedly. This doesn't seem right because it is kind of defeating the purpose right??. I believe the exectorService has to be created only once and that should be reused. Can you let us know what is the right model of instantiating the executorService and reusing the thread. And also when should any kind of shutdown on the executorService be used at all??

  • @NatureIsBeingLoved
    @NatureIsBeingLoved 4 года назад

    Ultimate explanation, just one doubt in case of FixedThreadPool and SingleThreadPool keepalivetime is 0 second which is little bit confusing, should it not be infinite or untill unless main application is up.

  • @nasrs3901
    @nasrs3901 5 лет назад +1

    awesome

  • @irshadahmad123456
    @irshadahmad123456 5 лет назад +2

    Excellent Video, something i was looking forward to.
    There is some confusion at 5:19. It says Que type is LinkedBlockingQue for fixedThreadPool and singleThreadExecutor. Wouldn't it be better to useArrayBlockIngQue since we now the size before hand and it will no go beyond the mentioned size?
    Anyone would like to comment ? Would be great if maker of this video can guide me in this matter :)

    • @DefogTech
      @DefogTech  5 лет назад +3

      The size of the threads (which will execute the tasks) is fixed but the pool doesn't know the number of tasks that will be submitted. Also, even if we have that info, the queue stores tasks temporarily only when all threads are busy executing previous tasks. The threadpool does not even know which task will take how long.
      Thus LinkedBlockingQueue makes more sense

    • @irshadahmad123456
      @irshadahmad123456 5 лет назад +1

      @@DefogTech great explanation, updated my knowledge with yours, thanks again for bringing up such a great peice of work, it's hard to find on Internet

  • @vvsnmurty
    @vvsnmurty 5 лет назад

    very good explanation, if you have time please publish Java8 features like streams.

    • @DefogTech
      @DefogTech  5 лет назад

      Hi, I have a video about Java 8 streams. Please check out the channel

  • @avijitmaiti07
    @avijitmaiti07 5 лет назад +2

    Hello. Thanks for the detailed explanation of the Thread Executor. Already subscribed your channel for upcoming videos.
    I have one question here - For the ShutdownNow() you have mentioned that it'll complete all the tasks which Threads are running and won't wait for the Queued tasks. But in the Javadoc to shutdownNow() method it's saying - 'Attempts to stop all actively executing tasks, halts the processing of waiting tasks, and returns a list of the tasks that were awaiting execution. These tasks are drained (removed) from the task queue upon return from this method.
    This method does not wait for actively executing tasks to terminate. ' Please advise here.
    Thanks in advance.

    • @DefogTech
      @DefogTech  5 лет назад +1

      shutdown() will allow existing tasks to complete and will not accept any new tasks. The method immediately returns though. Thus its recommended to use awaitTermination after using this method.
      shutdownNow() will interrupt existing tasks running, will return tasks waiting in queue, and not accept any new tasks.

    • @avijitmaiti07
      @avijitmaiti07 5 лет назад

      @@DefogTech Thanks for your clarification. Much appreaciated. :)

  • @mpvkarthick
    @mpvkarthick 4 года назад

    Custom Pool uses ArrayBlocking Queue. This means the Queue size is fixed or rather bounded. If the queue gets full, what is the purpose of creating a new thread? The Task will get rejected right?

  • @TheGuroguro12
    @TheGuroguro12 5 лет назад +1

    Thank you very much.

  • @360Legion
    @360Legion 6 лет назад

    Very very informative

  • @richardwang3438
    @richardwang3438 5 лет назад

    in the table where you compare different Queue Types, you mentioned for LinkedBlockingQueue "since queue can never become full, new threads are never created".
    actually the default capacity of LinkedBlockingQueue is Integer.MAX_VALUE, in theory it can become full. so I don't understand the reason of using LinkedBlockingQueue instead of ArrayBlockingQueue here.

  • @adonisaseem
    @adonisaseem 2 года назад

    8:57 How interesting is that!

  • @chary2933
    @chary2933 4 года назад

    Amazing bro

  • @shasankav8232
    @shasankav8232 6 лет назад

    Super..very nice explaination

  • @pengliu9987
    @pengliu9987 4 года назад

    Please note that the keep-alive-time for ScheduledThreadPool is not 60 seconds. For Java 8, it is 0 nano-second. For Java 13, it is 10 millisecond. It is very clear from the source code.
    //Java 8:
    public ScheduledThreadPoolExecutor(int corePoolSize) {
    super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
    new DelayedWorkQueue());
    }
    //Java 13:
    public ScheduledThreadPoolExecutor(int corePoolSize) {
    super(corePoolSize, Integer.MAX_VALUE,
    DEFAULT_KEEPALIVE_MILLIS, MILLISECONDS,
    new DelayedWorkQueue());
    }
    where DEFAULT_KEEPALIVE_MILLIS = 10L.

  • @yuvarajgaddam8217
    @yuvarajgaddam8217 Год назад

    what is blocking queue?

  • @nitantjoshi8614
    @nitantjoshi8614 4 года назад

    Can you provide the ppt ?? So that we can quickly refer

  • @vaibhavsawant20
    @vaibhavsawant20 6 лет назад

    Good content

  • @sadabhasan8218
    @sadabhasan8218 6 лет назад +1

    very good explanation and depth knowledge of executor service please make more video on Currency API Thanku sir

    • @DefogTech
      @DefogTech  6 лет назад

      Sure. More videos are coming.. did you check out the have concurrency playlist? It has many topics covered

    • @sadabhasan8218
      @sadabhasan8218 6 лет назад

      @@DefogTech please send me the link concurrency playlist

    • @DefogTech
      @DefogTech  6 лет назад

      Here you go - ruclips.net/p/PLhfHPmPYPPRk6yMrcbfafFGSbE2EPK_A6

  • @yauriattamimi4435
    @yauriattamimi4435 6 лет назад

    Thanks, your explanation is very clear. By the way, i got one question here. Regarding the shutdown of ExecutorService, we know that it will guarantee that all pending/waiting threads will be executed before the executor is terminated (graceful shutdown). In that case, what happen if lets say, our application got terminated by an unknown process (like process kill)..., will it be guarantee as well (that all pending/waiting threads got executed in the queue / pool) ?

    • @DefogTech
      @DefogTech  6 лет назад

      If process is killed, all threads will be immediately stopped including the thread pool, thus there is no guarantee that pending jobs will run

  • @paridhikhare1737
    @paridhikhare1737 5 лет назад +1

    how do I tell my program which policy to use ?

    • @DefogTech
      @DefogTech  5 лет назад +1

      There is a constructor which accepts the policy we want to use

  • @amriteshchandan
    @amriteshchandan 5 лет назад

    For ScheduledThreadPool, it looks like keepAliveTime is 0 NanoSeconds...
    public ScheduledThreadPoolExecutor(int corePoolSize) {
    super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
    new DelayedWorkQueue());
    }

  • @TheNeethz
    @TheNeethz 3 года назад

    can you provide with the slides please

  • @sagarshekhar6296
    @sagarshekhar6296 6 лет назад

    Are Rejection policies considered only in case of bounded queues or there is any possibility for Unbounded Queues(LinkedBlockingQueue) also?

  • @dm1817
    @dm1817 2 года назад

    謝謝!

    • @DefogTech
      @DefogTech  2 года назад

      Thank you for the support! Really appreciate it

  • @pradhumansingh1195
    @pradhumansingh1195 3 года назад

    Why the maxPool Size of a scheduledThreadPoolExecutor is Integer.MAX_VALUE? Anyone?

  • @sharanyarai378
    @sharanyarai378 6 лет назад

    What is diff b/w awaitTermination and shut down if both will execute all task in the queue?

    • @DefogTech
      @DefogTech  6 лет назад +2

      shutDown method will just initiate the shut down (thread won't block). ThreadPool will stop accepting new tasks.
      awaitTermination blocks the current thread for the duration mentioned (or before if all pending tasks are completed before that time).
      Thus, generally, shutdown is called first, then we use awaitTermination to allow tasks to complete. These are complementary methods.

  • @Sri7199
    @Sri7199 5 лет назад

    incase "Discard" policy, what will happen to the caller request?

    • @DefogTech
      @DefogTech  5 лет назад

      caller will not know.. it will be silently discarded without any exceptions thrown

  • @coding_ss632
    @coding_ss632 5 лет назад +1

    Can we get PPT's for the lectures?? It would be really very helpful.

  • @daumtto
    @daumtto Год назад

    개념추천

  • @shivasai362
    @shivasai362 3 года назад

    awesome