Java 21: Virtual Threads - A different async/await explained with Code Examples

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

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

  • @Phauglin99
    @Phauglin99 7 месяцев назад +3

    Excellent explanation and visualization on an actual problem, not just printing "Hello World" lines. Thank you!

  • @josedavidforero
    @josedavidforero 4 месяца назад +1

    Very clear explanation, thanks a lot.

  • @yeshengwei79
    @yeshengwei79 Год назад +4

    Very informative! Well done👍

    • @th30z-code
      @th30z-code  Год назад +1

      Thanks! I'm glad you enjoyed it

  • @thilankaliyanaarachchi984
    @thilankaliyanaarachchi984 6 месяцев назад

    Quality content! Please keep them coming :)

  • @SuperShahidAkhtar
    @SuperShahidAkhtar Год назад +4

    Thanks for examples

    • @th30z-code
      @th30z-code  Год назад +1

      Thanks for checking them out! I hope they were helpful.

  • @manojBadam
    @manojBadam 8 месяцев назад

    Very good explanation

  • @Anbu_Sampath
    @Anbu_Sampath 11 месяцев назад +1

    Nice examples.

  • @Asingh42
    @Asingh42 Год назад +2

    Now spring boot 3.2 supports virtual threads make a video on it how to use @Async with it

  • @Polly10189
    @Polly10189 Месяц назад

    Well done!

  • @RandomHandle120
    @RandomHandle120 6 месяцев назад

    The whole point of async/await in other languages is code cleanliness. None of this addresses Java's biggest shortcoming: nested/callback code hell!

    • @th30z-code
      @th30z-code  6 месяцев назад

      Java itself doesn't have many APIs with callbacks. all I/O and locks APIs, for example, looks synchronous.
      The problem is that since Java 8, lambdas and streams got abused (even in my examples, to quickly create functions).
      Also most of the "network framework" out there are still not leveraging virtual threads, since it's a major redesign to move away from the all the callback hell created to workaround async I/O limitations.
      But now the language allows to write cleaner code, so it's just a matter of choosing a newer/better set of libraries.

    • @RandomHandle120
      @RandomHandle120 6 месяцев назад

      @@th30z-code You still can't, for example, write sequential-looking code that sends an HTTP request without blocking the UI thread. This was a problem in every language a while ago, and many fixed it by switching to an async/await model. Java refused to do anything about it, so to this day, you still have to write uglier, unmaintainable code riddled with callback lambdas to keep your app's UI responsive.

    • @th30z-code
      @th30z-code  6 месяцев назад

      can you share a bit more? I haven't used any java UI framework lately, but my guess is that is just a "UI framework problem", meaning that the framework is still not leveraging virtual threads.
      If your "onClick()" for examples is run in a virtual thread you can use the HttpClient.sendSync() client, take locks and do other blocking stuff with a code that will look sequential and not blocking the UI.

    • @RandomHandle120
      @RandomHandle120 6 месяцев назад

      @@th30z-code It's not a framework/library problem, it's a problem with the language itself. It doesn't matter what framework you're using, you simply cannot write sequencial, yet asynchronous code in Java until Oracle decides to implement some sort of async/await feature onto the language itself; until then, Java remains a terrible language to write asynchronous code in.

    • @th30z-code
      @th30z-code  6 месяцев назад

      virtual threads are the async/await alternative.
      In the way they implemented there is no reason to add new keywords like async/await.
      java takes care of scheduling the threads by itself, since underneath is known which operations are "blocking I/O" and which are not. so you can think as it as "java adds the await where it is needed".
      so, if your code/framework uses virtual threads you now can write sequential code and it will be the same as using async/await in other languages.
      just try to write a simple example with a Thread.sleep() or an httpClient.send(), you'll see that it will actually look nice even without the async/await keyword.