multithreading - Java - Threading HttpUrlConnection calls -


i have custom service module pass json body, containing array of ids. have iterate through these ids , make separate web service call each id obtain response body, aggregate these responses custom json structure. have working, i'd implement threading (or manner thereof) make http calls asynchronously, rather in succession. how implement threading in following code:

    ids = (jsonarray) jsonin.get("ids");          myclass myclass = null;     list<myclass> myclasslist = new arraylist<myclass>();      (int = 0; < ids.size(); i++) {         jsonobject p = (jsonobject)ids.get(i);         id = p.get("id").tostring();          //the httpurlconnection call made in getresponse() method         gson gson = new gson();         myclassresponse result = gson.fromjson(getresponse(),                 myclassresponse.class);          (int x = 0; x < result.ids[0].id.length; x++) {              myclass = new myclass();              myclass.setstringone(result.ids[0].fieldone);             myclass.setstringtwo(result.ids[0].fieldtwo);              myclasslist.add(x, myclass);         }                }      gson gsonout = new gson();     string jsonstring = gsonout.tojson(myclasslist);      jsonarray jsonarray = new jsonparser().parse(jsonstring).getasjsonarray();      jsonobject response = new jsonobject();     response.put("customstructure", jsonarray);      //pass custom json body 

use logic:

  • create runnable loop doing, constructor taking individual jsonobject input.
  • store output myclassresponse member variable in runnable.
  • have list of threads , list of runnable declared outside loop
  • create thread , runnable inside loop , add lists
  • start thread inside loop
  • after loop call thread.join on each of threads in list of threads (this makes thread wait till threads completed.)
  • create myclasslist list of runnables once join statements have returned back

Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -