• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How can i handle to get result from completableFuture without block ?

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have code like this and have one question , how can i handle method get block untill have result ,
Should I run it in separate thread??


 
Marshal
Posts: 79275
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not sure I understand the question. Your method will block the other thread until the 3 seconds complete. You are already running that code on a separate thread, that called “main.”
 
Kieu Thoai
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So what if I want it to print the word blocked first and then get the result?
 
Campbell Ritchie
Marshal
Posts: 79275
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Added you to two other fora.
Your variable names are difficult to understand; please call the Integer result or similar. Please learn how to break long lines into shorter lines. Please don't use new Integer(...); use Integer.valueOf(...) instead. You could change line 14 to read return 10; and allow boxing conversion to occur.
 
Kieu Thoai
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I handled it by running the get() function in another thread? I don't know if there is a better solution?
 
Campbell Ritchie
Marshal
Posts: 79275
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The whole idea is that the main thread will wait for the CompletableFuture to finish its work. There are two sources I suggest you read.
  • 1: The documentation for CompletableFuture.
  • 2: The chapter in Modern Java in Action (and its predecessor books) by Urma Fusco and Mycroft (Manning 2017(?)).
  • I think you will find No 2 very helpful.
     
    Sheriff
    Posts: 22784
    131
    Eclipse IDE Spring VI Editor Chrome Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Kieu Thoai wrote:So what if I want it to print the word blocked first and then get the result?


    By creating a new CompletableFuture using one of its methods. In this case, since you want the original value, thenApply fits best:
     
    Campbell Ritchie
    Marshal
    Posts: 79275
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Kieu Thoai wrote:. . . print . . . blocked first . . .

    You can put a println() statement before where you call get(). I don't know how useful that will be. You will still have the 3 second delay.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic