• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Use of join method in CompletableFuture class vs get method

 
Ranch Hand
Posts: 101
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wanted to implement a functionality where a big file gets broken down into chunks and processing can happen in parallel.

I used CompletableFuture to run tasks in parallel. unfortunately , it doesnt work unless i use join. Im surprised that this is happening, since according to docs, get is also a blocking methd in the class. can someone please help me in figuring out what  i am doing wrong





my file contents is pretty small:

  • 1212451,London,25000,Blocked
    1212452,London,215000,Open
    1212453,London,125000,CreditBlocked
    1212454,London,251000,DebitBlocked
    1212455,London,2500,Open
    1212456,London,4000,Closed
    1212457,London,25100,Dormant
    1212458,London,25010,Open
    1212459,London,27000,Open
    12124510,London,225000,Open
    12124511,London,325000,Open
    12124512,London,425000,Open
    12124513,London,265000,Open
    12124514,London,2577000,Open
    12124515,London,2504400,Open






  • in case i uncomment the above line in the method processChunk, everything works fine. my values are printed and everything. however if i remove it, nothing happens. all i get are notifications that futures have copleted but the contents are not printed


    This is my output
     
    Saloon Keeper
    Posts: 13280
    292
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    The fact that it works at all is more luck than anything else.

    A big part of the problem is that you're passing the same list instance to every asynchronous task handler and clearing it instead of creating a new one. So not only does every handler work with the same list of lines, the lines are cleared at the same time that the processing begins.

    Let's first create a reusable collector that will batch your lines:

    You can then very easily achieve what you want with the following code:
     
    Stanley Walker
    Ranch Hand
    Posts: 101
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Stephan van Hulst wrote:The fact that it works at all is more luck than anything else.

    A big part of the problem is that you're passing the same list instance to every asynchronous task handler and clearing it instead of creating a new one. So not only does every handler work with the same list of lines, the lines are cleared at the same time that the processing begins.



    thank you stephan for your response.  i actually corrected that piece using



    I appreciate your code sample, pretty impressive.
    however my main question is regarding join. will completablefutures not run unless join is called?
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 13280
    292
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    They do. You're just not getting any output because you clear the list that the asynchronous task have to work with, so they don't print anything.
     
    A teeny tiny vulgar attempt to get you to buy our stuff
    Thread Boost feature
    https://coderanch.com/t/674455/Thread-Boost-feature
    reply
      Bookmark Topic Watch Topic
    • New Topic