java - joining results after making parallel calls using spring async -
i'm using spring @async make parallel calls same method/backend. there clean way join results in list? pseudo code came make calls/merge result, i'm not sure it's cleanest way.
list<future<string>> futurestringlist = new arraylist<future<string>>(); for(each backend call){ future<string> futurestring = backend.retrieve(); futurestringlist.add(futurestring); } list<string> actualstringlist = new arraylist<string>(); while(!futurestringlist.isempty()){ for(future<string> futurestring: futurestringlist){ if(futurestring.isdone()){ actualstringlist.add(futurestring.get()); futurestringlist.remove(futurestring); } } }
future.get()
blocking call, can loop on future
list , know that, @ end of loop, futures finished.
list<future<string>> futurestringlist = new arraylist<future<string>>(); for(each backend call){ future<string> futurestring = backend.retrieve(); futurestringlist.add(futurestring); } list<string> actualstringlist = new arraylist<string>(); (future<string> stringfuture : futurestringlist) { actualstringlist.add(stringfuture.get()); // blocking call } // strings retrieved @ point
Comments
Post a Comment