i'm trying use parallel streams call api endpoint data back. using arraylist<string>
, sending each string method uses in making call api. have setup parallel streams call method call endpoint , marshall data comes back. problem me when viewing in htop see cores on db server light second hit method ... first group finish see 1 or 2 cores light up. issue here think getting result want ... first set of calls , monitoring looks rest of calls made 1 @ time.
i think may have recursion i'm not 100% sure.
private void generateobjectmap(integer count){ arraylist<string> mylist = getmylist(); mylist.parallelstream().foreach(f -> performapirequest(f,count)); } private void performapirequest(string mystring,integer count){ if(count < 10) { treemap<integer,treemap<date,myobj>> tempmap = new treemap(); try { tempmap = myjson.gettempmap(myrestclient.executeget(mystring); } catch(sockettimeoutexception e) { count += 1; performapirequest(mystring,count); } ... else { system.exit(1); } }
this seems unusual use parallel streams. in general idea informing jvm operations on stream independent , can run in order in 1 thread or multiple. results subsequently reduced or collected part of stream. important point remember here side effects undefined (which why variables changed in streams need final or final) , shouldn't relying on how jvm organises execution of operations.
i can imagine following being reasonable usage:
list.parallelstream().map(item -> getdatausingapi(item)) .collect(collectors.tolist());
where api returns data handed downstream operations no side effects.
so in conclusion if want tight control on how api calls executed recommend not use parallel streams this. traditional thread
instances, possibly threadpoolexecutor
serve better this.
Comments
Post a Comment