Тёмный

Introduction to CompletableFuture in Java 8 

Defog Tech
Подписаться 85 тыс.
Просмотров 326 тыс.
50% 1

Опубликовано:

 

29 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 338   
@prashantjha439
@prashantjha439 3 года назад
Trust me this channel is best for industry people.....the sad part is we do not get to see any more videos from this channel.......i would request the creator to at-least think for a membership based subscription
@BairagiBiker
@BairagiBiker 4 года назад
I haven't seen any other tutorial that has described this with such simplicity and beauty!! Really great work man!
@sekharsamanta6266
@sekharsamanta6266 6 месяцев назад
The most practical video I've ever seen on CompletableFuture
@kunalrai7577
@kunalrai7577 3 года назад
Your explanations are truly amazing and crystal clear, hats off and thanks a lot for such a great content.
@vishall3379
@vishall3379 4 года назад
Hi Deepak, Your doing a excellent job of covering the most complex topic of Java threads & concurrency on your channel by providing detailed insight & explanation on each feature in simple and clear manner with hands on code. Keep it up.👍
@asashish905
@asashish905 2 года назад
Whenever I think of concurrency, I know which YT channel to look up. 🙂❤
@ramt179
@ramt179 2 года назад
Crystal clear. You are really have the knack of teaching
@davesachin1048
@davesachin1048 Год назад
Thanks.. you really made this complex thing easy!!!
@sivaramakrishnakv
@sivaramakrishnakv 3 года назад
Very composed and clear explanation with practical examples. Great work!!
@aryansharma6725
@aryansharma6725 Год назад
Such great understanding, I have become a fan. How did you learnt about all these ,you prefer documentation or books or videos? Knowing the answer to this will help me hit the foundational part missing in my learning. Great video though.
@SreekarAnugu
@SreekarAnugu 4 года назад
You deserve more subscribers and views. Very clear and crisp and to the point.
@KundanRoy
@KundanRoy 5 лет назад
You are awesome man.. i just can't explain how much you helping people to understand deep concepts in easy way. I request you to make series of RxJava ..
@david3979
@david3979 2 года назад
Thank you for the time and effort!
@sateesh9525
@sateesh9525 3 года назад
Very good explanation,
@edarasudhakar8654
@edarasudhakar8654 2 года назад
Amazing explanation
@jyotikumarpoddar8048
@jyotikumarpoddar8048 4 года назад
Crystal clear explanation 👍
@conscious-universe
@conscious-universe 2 года назад
super clear! thank you
@surendralamichhane9387
@surendralamichhane9387 4 года назад
Awesome explanation. Looking forward to your other tutorials.
@ayusharora8141
@ayusharora8141 3 года назад
Very well explained. Thanks !
@ihort6739
@ihort6739 2 года назад
Great tutorials, thanks man
@Rrffg695
@Rrffg695 2 года назад
just amazing
@TheHarpanOnly
@TheHarpanOnly 4 года назад
Very clear explanation. Thanks a lot.
@sky-ec3hi
@sky-ec3hi 2 года назад
Awsome video, buddy!!
@sanjaygarg3540
@sanjaygarg3540 4 года назад
Best Explanation.....
@kotetechuse7254
@kotetechuse7254 4 года назад
Thank you... Keep up the good work Sir 👌🏻
@gunjanshrivastava8372
@gunjanshrivastava8372 3 года назад
Please add a video for uncauthExceptionHandler
@lella2083
@lella2083 3 месяца назад
Even in supply async the other thenapplu operations are dependent on the previous one right ? What if the it is waiting for the value of the first one
@sagartyagi2450
@sagartyagi2450 2 года назад
Hey, thanks for the awesome tutorial. I've one question, why do you even need CompletableFuture, why can we just create a TASK which handles all the sequential flow using futures and start multiple such tasks itself?
@TanujChauhan999
@TanujChauhan999 Год назад
thanks
@akshaykumar-uv3up
@akshaykumar-uv3up 3 года назад
what are the constraints of completable future and how we can solve in huge projects.
@tejeswarsahu2498
@tejeswarsahu2498 5 лет назад
U have explained it in the simplest way...Thank you
@arnabthakuria2243
@arnabthakuria2243 4 года назад
Thanks man . You are amazing
@rakeshkr2
@rakeshkr2 Год назад
14:49 cpuBound & ioBound ExecutorService need to be declared outside the for loop right ?.
@divyamahesh9032
@divyamahesh9032 2 года назад
Thanks
@samsudha5674
@samsudha5674 6 лет назад
It is very good explanation...
@DefogTech
@DefogTech 6 лет назад
Thank you!
@ragupathia2316
@ragupathia2316 6 лет назад
Very nice article bro
@sachinbijwe6857
@sachinbijwe6857 5 лет назад
Great Explanation.. Thanks a lot
@ninadsentinel
@ninadsentinel 3 года назад
How about using ExecutorCompletionService to overcome the problem of future.get calls. I feel a better option.
@jww7663
@jww7663 5 лет назад
Thank you! very clear
@DefogTech
@DefogTech 5 лет назад
You're welcome :)
@MohanRam-sw6tm
@MohanRam-sw6tm 4 года назад
Hi, Do we have anything like get() in future here? If it there in completablefuture what is that & still that will block tha main thread?
@DefogTech
@DefogTech 4 года назад
yes it does have blocking get method
@bigjforever
@bigjforever 5 лет назад
Please make videos on RxJava
@AxeTvCommunity
@AxeTvCommunity 2 года назад
💯
@anushkasharma2729
@anushkasharma2729 6 лет назад
Great Job With the explanation. Though i have a question. At 15:50 when you mark enrich method to be async, how does that work in real world ? As far as i understand marking a method async means it can run on it's own without being dependent on anything else. But in this scenario it actually requires output from getOrder as input in Enrich. So does that mean it still waits until getOrder gives a output and then runs it irrespective of which thread pool it runs in ?
@DefogTech
@DefogTech 6 лет назад
That's correct. The whole code is like an algorithm where each step of execution requires input from previous step and generates output for next step. Thus enrich will not even be assigned a thread until fetch order is completed. Once order task gives output, a new or same thread is assigned to enrich and that order is given as input.
@venkatnani6940
@venkatnani6940 3 года назад
it is like promises in javascript
@balakrishnajangita6638
@balakrishnajangita6638 5 лет назад
I don't have words to describe you boss awsome bro
@gkcs
@gkcs 4 года назад
10:47 I got unsettled watching those lambdas not being converted to method references :P Thanks for the great video Deepak!
@B-Billy
@B-Billy 4 года назад
@Gourav are you a java developer? :)
@upeshsai2804
@upeshsai2804 4 года назад
Wow it's soo good to see you here Gaurav ! It's like when both of my gurus meet :D
@marlonemoreira3460
@marlonemoreira3460 3 года назад
Ra
@ms_thesingh7894
@ms_thesingh7894 3 года назад
By voice it looks like Gaurav and Deepak are same :-)
@chetanmishra7009
@chetanmishra7009 3 года назад
@@ms_thesingh7894 gangadhar(#guru_ji) hi shaktimaan hai
@drakezen
@drakezen 6 лет назад
Would be great if you had a series on reactive programming. Good complement to your discussion on completablefutures
@TheHarpanOnly
@TheHarpanOnly 4 года назад
I do agree. You have very good approach in teaching other.
@surajsingh-vu5oc
@surajsingh-vu5oc 3 года назад
Please make video on reactive programming
@mostinho7
@mostinho7 4 года назад
DONE thanks 1:17 callable is used when you want to return a value from a thread (normal thread doesn’t return anything) Implementing call method that returns ReturnType Callable returns a Future that is a placeholder for the real value. Doing future.get() is blocking if the task is not completed 2:50 visualization We can chain methods together (instead of blocking with get() we say thenApply(...) and chain calls asynchronously See chaining from 5:59 12:50 can perform the chained asynchronous operations on the same thread or provide a different thread pool (executor service) for each operation (some operations might be cpu intensive some io, so we want to use different threadpool) 17:00 we don’t have to specify an executor to completable future, it uses forkjoinpool internally Todo continue from 1:17
@heshamosman9752
@heshamosman9752 4 года назад
Whenever I want to remember something about java concurrency, I go and watch your videos, Great job
@pankajgupta4740
@pankajgupta4740 4 года назад
Terse, succinct, precise, informative, amazing, clear, .... running out of words to describe. Thank you so much for the video series
@dhaanaanjaay
@dhaanaanjaay 2 месяца назад
why this channel stopped. This is the best source for concurrency topics.
@anandashekhar9908
@anandashekhar9908 Год назад
Do you have any video tutorial about reactive framework in Java please (Extension RX Java)? If yes please kindly share the link.
@vivekpurushothaman8
@vivekpurushothaman8 5 лет назад
Brilliant video. A lot of my confusions got resolved through this video. I will subscribe to you because I find learning with you very enriching. Please keep creating more videos like this :) Also for completablefuture if you can include collecting multiple futures through .get .all etc... It'll be complete
@GauravDubey-om7lr
@GauravDubey-om7lr 10 месяцев назад
@15:07 creating a threadpool for every task seems like a overkill . Can we not create threadpool outside of loop it might reduce overhead of 100 threadpools
@gersonadr2
@gersonadr2 Год назад
Finally I understood! Thank you!!
@ndk8111
@ndk8111 Год назад
Thank you for the session. A few questions please: ONE - what if instead of creating a loop of 1:100 (i) you fetched 100 product ids, how would you revise your code to process 100 orders in parallel? TWO : what if you needed a receipt at the end of each order processing (success or failure) , how would you aggregate results of each thread without blocking and print the final 100 line status receipt from main thread?
@HighlifeC
@HighlifeC Год назад
I am interested to smash the like button..in a for loop 100 times in async mode :)
@Javavidz
@Javavidz Год назад
I have read lot of books and watched so many videos on multi threading, no one explained in this manner. Great work. Thanks for the content.
@virendrakhade12
@virendrakhade12 6 лет назад
Very nice explanation, thanks a lot
@DefogTech
@DefogTech 6 лет назад
You're welcome!
@krishandeo4070
@krishandeo4070 3 года назад
Hello sir, I am big fan of your teaching skill. You explain tough topics in very easy way. I request you to please make some tutorials on RxJava. Thank you so much for all your effort.
@hyperborean72
@hyperborean72 4 года назад
Hello, thank you for the great tutorial. Did I understand right that in your example of single order processing the tasks submitted to ExecutorService are blocking to each other though each one is performed in a separate thread? if I'm correct I was wondering why did you use ExecutorService for single order processing if single order processing is sequential by its nature. And the problem is how to fire several separate order processings - not to separate different steps of the same order processing?
@manishmanghwani731
@manishmanghwani731 3 года назад
The way you explain complex things in simple flow with sharp voice makes it impossible to take break from learning :) Great work Sir!
@KanagaveluSugumar
@KanagaveluSugumar Год назад
What to do if i have blocking operation in my task? Say IO Operation. Do i still use the CompletableFuture? if it blocks my executor thread pool then will that be acceptable?
@ibnlopqrt
@ibnlopqrt 9 месяцев назад
Yes you can use it
@MakeItStik
@MakeItStik 4 года назад
Another beautiful and clearly explained video :) Haven't seen any new videos from you :( Please continue making such videos, it really helps.
@cseshivaprasad1985
@cseshivaprasad1985 6 лет назад
Wonderful explanation with right example. I was always struggling to apply this pattern for my processes, this gives lot of clarity. Is it a simplified version of Saga pattern ?
@karanvatwani9077
@karanvatwani9077 3 года назад
Very well explained. One doubt i have, suppose if we have one executor service and we have Future.submit(task) which is performing some DB fetch or list operation and when we do future.get() with timeout overloaded method so it is blocking the thread and giving Timeout Exception. We are passing 120 sec time out limit in get() method. So how does CompletebaleFuture.suuplyAsync() and CompletableFuture.complete() will help us here?
@DurgaShiva7574
@DurgaShiva7574 Год назад
salute you from heart.. no words i have !
@MukeshShah-lj4th
@MukeshShah-lj4th Год назад
Sir your explaination is awesome. I haven't see this type of deep explaination from other tutorials
@adityagarg9806
@adityagarg9806 5 лет назад
what if i put all the tasks in run method in sequence and then start it by executor framework i mean i can achieve same thing
@navjot7397
@navjot7397 4 года назад
In your case, these tasks will not be executed asynchronously since you are putting them in a single method (run()), that's why you need to create separate methods or objects for each of a task, so they can be computed in async
@bandisruthi6877
@bandisruthi6877 3 года назад
Really nice 👌.. I have never seen this type of videos related to concurrent package...Awesome explanation with simple examples ...tq so much....
@rohannayak4979
@rohannayak4979 4 года назад
Awesome explanation Sir. Why are there 20+ down votes....?
@asankasiriwardena3383
@asankasiriwardena3383 Год назад
Wow superb explanation ..!
@RaviBoyina
@RaviBoyina Год назад
super precise and super cool..thanks to you sir.
@vijaykumarreddyt3287
@vijaykumarreddyt3287 Год назад
You are doing Great service, Dude
@gajendralnmiit
@gajendralnmiit 4 года назад
The below code gives the output: CompletableFuture future = CompletableFuture.supplyAsync(() -> { System.out.println(Thread.currentThread().getName()); return "Hello "; }).thenApply((i) -> { System.out.println(Thread.currentThread().getName()); return i + "World "; }); output:- ForkJoinPool.commonPool-worker-1 main Hello World while below one gives:- CompletableFuture future = CompletableFuture.supplyAsync(() -> { System.out.println(Thread.currentThread().getName()); return "Hello "; }).thenApplyAsync((i) -> { System.out.println(Thread.currentThread().getName()); return i + "World "; }); output:- ForkJoinPool.commonPool-worker-1 ForkJoinPool.commonPool-worker-1 Hello World It contradicts with your statement that supplyAsync and thenApply operations will be executed in separate threads other than main thread!
@akshaykumar-uv3up
@akshaykumar-uv3up 3 года назад
Hi, I am trying append the string with exception. Even though there is an exception.it is performing the next step(passing the previous output into next input of applyasync(()) method.(Sequence of task's) Once the exception is raised shouldn't continue. could you please tell me how to handle this. output: main pool-1-thread-2 pool-1-thread-3 java.lang.NullPointerException how are you find the below snippet. package multithreading.completablefuture; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.function.Function; public class CompletableFutureTest { public static void main(String[] args) throws InterruptedException, ExecutionException { System.out.println(Thread.currentThread().getName()); //Create New Thread pool ExecutorService newCachedThreadPool = Executors.newCachedThreadPool(); String s=null; //pass newCachedThreadPool to the completableFuture.so that completableFuture takes threads from newCachedThreadPool CompletableFuture completableFuture = CompletableFuture.supplyAsync(()->s,newCachedThreadPool) .thenApplyAsync( new Function() { @Override public String apply(String s) { System.out.println(Thread.currentThread().getName()); return s.toLowerCase(); } },newCachedThreadPool).exceptionally( e-> e.getMessage()) .thenApplyAsync( new Function() { @Override public String apply(String s) { System.out.println(Thread.currentThread().getName()); return s+" how are you"; } },newCachedThreadPool); System.out.println(completableFuture.get()); } }
@amitsinha3944
@amitsinha3944 3 года назад
Beautifully explained. Have one confusion though. Would request you to clear that. I tried below piece of code to get complete understanding. What i was expecting is all threads to complete except the one for which i purposefully slept thread for 5 seconds. for(int i=0;i"").thenAccept((o)->{ if(val==5){ try(){ Thread.sleep(5000); }catch(InterruptedException e){ } } System.out.println(o+"-printed-"+val) }) } However, i saw o/p like this which caused the confusion amit-printed-0 amit-printed-1 amit-printed-2 amit-printed-3 amit-printed-4 waited for 5 seconds and then printed.. amit-printed-5 amit-printed-6 amit-printed-7 amit-printed-8 amit-printed-9 I thought no thread will be blocked if any one cause delay. In order to fix that i had to provide a seperate threadpool like below ExecutorService executorService =Executors.newCachedThreadPool(); CompletableFuture.supplyAsync(()->"",executorService).thenAccept((o)->{... Can you please clear this confusion.
@tristanmoller9498
@tristanmoller9498 2 года назад
100 orders are placed inside the same thread pool. As soon as the first task, ( here: (() -> getOrder()) ) finishes, will the thread pool immediately continue work on that future object, so in essence continue with the next task from that future object, which would be (order -> enrich(order)). Or will (order -> enrich(order)) be placed at the end of the thread pool task-queue and the thread pool will first complete the task (() -> getOrder()) 100 times?
@kunalrai7577
@kunalrai7577 3 года назад
Dear Sir , I feel its just an another form of Function Functional Interface , where andThen is replaced by thenApply Above code can also be written as order.andThen(price.andThen(confirmationMessage)).apply(9);
@sushmithashenoy7581
@sushmithashenoy7581 Месяц назад
Beautifully explained. You make everything very simple yet covering all thats relevant to the context. Please keep up the good work that benefits millions like us.
@shwetapriyadarshani197
@shwetapriyadarshani197 Год назад
Too concise and crisp 👍
@vikasking1
@vikasking1 3 года назад
How would it be different from creating array of futures each having task of completeOrder() and this completeOrder is internally divided into multiple methods of getOrder, enrichOrder, performPayment and so on.... ?
@kumarmanish9046
@kumarmanish9046 2 года назад
So why cant we have a wrapper method call that in turn calls the 5 methods one by one. And then That method implements callable and returns final result. That way, each thread will be executing a different order. And we would not need CompleteableFuture . What am I missing?
@vamsi0706
@vamsi0706 2 года назад
Hii... I have 3 queried need to run asyn....and need to wait for the results to store in list.... Then after 3 tasks done... I need to proceed further running the remaining code.... In this scenario...could anyone suggest which is better to use... I tried executor service...but it is going sync...not waiting for the threads to complete.... Thanks in advance
@kk-pi4hj
@kk-pi4hj 4 года назад
I understand the dependency issues is solved compared to regular Future but don't you still need to do completablefuture.get() after your for loop? otherwise your main thread will exit while the tasks in your completable future are still running .
@AshishRohillax
@AshishRohillax Год назад
Brilliant explanation mate.❤
@AbhijeetMuneshwar
@AbhijeetMuneshwar Месяц назад
This is 1 video which explained me Future vs Callable Future clearly out of many other videos
@lings628
@lings628 4 года назад
Fantastic job man! God bless you. Please do a video on RxJava. Nobody will be able to explain it like you do.
@jsaimanohar
@jsaimanohar Год назад
Really great work man!
@ayushgarg5929
@ayushgarg5929 2 года назад
Why is only first task is necessary to unblock main thread , what not all the future place holders should be filled first to unblock?
@pbgkhy
@pbgkhy Год назад
Superb explanation
@TheDEMMX
@TheDEMMX 2 года назад
If the tasks are dependent on each other how can we use applyAsync()? We need to wait for defendant task anyway, or do I not understand it correctly?
@solorankerone
@solorankerone 2 года назад
came back it to again after few years. still blows my mind how simply OP has explained the concepts.
@gargsuraj12
@gargsuraj12 3 месяца назад
Have to say, very well explained. Keep creating the good work.💪
@mayankpant5471
@mayankpant5471 Год назад
who is this guy ? I have seen very few people in internet explain java topics with such simplicity and cohesion
@rajeshg3570
@rajeshg3570 2 года назад
Beautiful explanation on this concept. I've a question here - Do we have any specific advantage if we use two thread pools instead of one? I mean to ask what are the practical scenarios to use more than one thread pool ?
@ashishtrivedi6426
@ashishtrivedi6426 3 года назад
If ordering is not important then we would have placed them in list and used invokeall()
@RahulKumar-j9y6p
@RahulKumar-j9y6p 11 месяцев назад
liked the detailing
@SunnyKumar-ud9gp
@SunnyKumar-ud9gp Месяц назад
Please please continue video making😭😭..it's a GOLD
@sa3882
@sa3882 4 года назад
Why we are using same order variable for every future, future1, f2 and so on,?
@AbhijeetMuneshwar
@AbhijeetMuneshwar Месяц назад
This video has shown me true power of CompletableFuture
Далее
What is Spring Webflux and when to use it?
17:46
Просмотров 243 тыс.
Катаю тележки  🛒
08:48
Просмотров 515 тыс.
ДЕНЬ УЧИТЕЛЯ В ШКОЛЕ
01:00
Просмотров 1,2 млн
CORTE DE CABELO RADICAL
00:59
Просмотров 2,3 млн
Circuit Breaker Pattern - Fault Tolerant Microservices
12:19
Understanding how ForkJoinPool works
13:16
Просмотров 122 тыс.
CompletableFuture in Java 8 -  (Part 1)
23:35
Просмотров 21 тыс.
Java Memory Model in 10 minutes
10:55
Просмотров 261 тыс.
ThreadLocal in Java
10:59
Просмотров 185 тыс.