Sync, Async, Blocking and Non-Blocking | Rock the JVM
Вставка
- Опубліковано 30 тра 2024
- Written form to keep for later: blog.rockthejvm.com/sync,-asy...
Using the controllable Futures pattern: • How to Write Controlla...
This video is for programmers of all levels (although I write Scala, naturally). We discuss how some computations are synchronous and blocking, async and blocking, and how we could achieve async non-blocking computations with Akka actors.
Async and non-blocking is what you want. However, even that has its drawbacks. So at the end I show how you can also obtain meaningful values out of a non-blocking computation.
Follow Rock the JVM on:
LinkedIn: / rockthejvm
Twitter: / rockthejvm
Blog: rockthejvm.com/blog
-------------------------------------------------------------------------
Home: rockthejvm.com
-------------------------------------------------------------------------
///////////////////////////////////////////////
// Libraries code for build.sbt:
val akkaVersion = "2.6.4"
libraryDependencies ++= Seq(
// akka streams
"com.typesafe.akka" %% "akka-actor-typed" % akkaVersion
)
///////////////////////////////////////////////
Timeline:
0:00 intro & requirements
0:47 synchronous, blocking
2:48 asynchronous, blocking
6:20 asynchronous, non-blocking with an actor
11:17 obtaining meaningful values - Наука та технологія
6:38 "Actor ... is not something active. It's just a data structure."
This enlightened me. Brilliant tutorial!
So glad it clicked!
Wow.. I read multiple articles without it clicking. I watched your video and felt like I understood it well before it was even over. Thanks a bunch, you're an awesome instructor
Thank you!
Thanks for these great videos. Do you have any resources on writing and debugging Futures with the ScalaTest framework? I've been trying to use IntelliJ's debugger and evaluate tool to follow the code paths. But I'm not getting through
Thank you for the fantastic work, Daniel! Btw, I'm just wondering if you have any plan to do a course on the akka typed actor and make it available on Udemy or RockTheJVM, as I'm really looking forward to it
On the roadmap - it will take some time as the material is massive, so thanks for the patience!
Awesome video!
One thing that i dont understand is if u take the example of making a HTTP request or call to a database.
I understand that if this call is made asynchronous, the calling thread can continue working, but the Thread waiting for the Future to complete is blocked.
However, if you do this with Actors, the fact doesn't change that by the nature of the HTTP request or database it will take some time to complete the result, so it still blocks the execution
somewhere, right?
We're concerned with whether the thread itself is waiting. If you fire the call and the thread moves to other tasks, it's not blocked.
this is not getting
another great video
It's an amazing video, thanks :)
in the last line you call asyncNonBlockingResult.onComplete(...) which will be executed in one of the global execution context’s threads.
question: how this thread will know about the future completion (which will be completed in the actor’s dispatcher’s thread pool)?
will the dispatcher’s thread somehow notify the globals’s thread that future is completed and is ready to be used in onCompleted(..) block?
Something like that. The execution context also has a small scheduler which is able to run callbacks and Future transformations like map/flatMap.
Good video, thank you!
One question, if you allow me. Let's say we have 1 processor, 1 core machine. Will Futures and Asyncs run concurrently, I mean, 1 thread jumps back and forth from one task to another, imitating concurrency, or will it be sequential operations with a fully determined order?
Nope, still concurrent. The OS can schedule and interrupt threads on the same physical core at (almost) any time, so the same conditions for multithreading still apply.
Thank you for this great tutorial. Could you please explain the syntax at 7.45. Specifically the syntax that confuses is me is the "someMessage=> {the function block}". As far as I understand the "=>" operator is used to define types. I.e. you can define the type definition of a function. So what does this syntax mean?
Check the Scala at Light Speed video that talks about functions, we have everything there.
Thanks💕💕💕
I am a big fan of your courses on RTJVM. I have a question: “Using actor is asynchronous and non-blocking”, in short, the idea is delegating “blocking thing” to actor’s dispatcher and benefit from its better scheduling, isn’t it?
You're not delegating anything to an actor - you're just sending it a message. The internal logic of the actor will run at some point, on some thread. The end goal is not necessarily better thread scheduling (although that is also true) but better isolation of logic in your application, without needing to care about threads at all.
Thank you, pretty nice like always. Do you have any plans to do a course about ZIO ?
Perhaps - shout in the comments if anyone else needs ZIO!
Zio +
Yes zio would be great 🔥
Do you think you made non-blocking operation just because you have removed from the third case "Thread.sleep(10000)" line which occured in first two cases? If I put this line to the code-block that will be executed by ActorSystem then there`ll be also a blocking. The only difference is that this blocking will block the dispatcher`s thread, but since dispatcher, global execution context and a main thread are placed on the same machine - then what`s the profit of the model (with promises) that you`ve shown?))))))))
The sleeps are just for illustration, I think you got the idea.
Very good lesson - interested in finding out why the application does not terminate. Is there some event-loop running?.. if so is there a 'stop' method.
Edit: The actor systems have to be terminated. rootActor.terminate() and promiseResolver.terminte()
There's the execution context running, which needs stopping.
Will the main thread not block if message is being sent to Remote actors mail box?
Never - message sending is fully non-blocking
haha nice parrot