java - Hit a url n times parallely -


i want hit url n times parallelly. googled , found this url. tried change according requirement(hit url n times , see response of each one),but not succeed.i added following code exacuting request , getting response in task class constructor in above link,but work searially.

defaulthttpclient client = new defaulthttpclient();   httppost method = new httppost(aurl);   try {       httpresponse httpresponse=client.execute(method);       stringbuffer buffer = new stringbuffer();       bufferedreader reader = new bufferedreader(new inputstreamreader(httpresponse.getentity().getcontent()));       string dataline = null;       while((dataline = reader.readline()) != null){             buffer.append(dataline);       }       string responsemsg =    buffer.tostring();       system.out.println(responsemsg);      

any appriciated.

you can employ threadpool number of threads equal number of parallel requests want send. wrap logic (creating , sending request) runnable task , create , submit instances of tasks using loop.

int numofthreads = ... <num of parallel requests generate> executorservice es = executors.newfixedthreadpool(numofthreads); (int = 0; < numofthreads; i++) {     es.submit(new task()); } 

your task somthing this. can whatever have within run() implementation. also, can improvise on setting off threads @ once using synchronizers.

class task implements runnable{     @override     public void run() {         defaulthttpclient client = new defaulthttpclient();         httppost method = new httppost(aurl);         try {             httpresponse httpresponse=client.execute(method);             stringbuffer buffer = new stringbuffer();             bufferedreader reader = new bufferedreader(new inputstreamreader(httpresponse.getentity().getcontent()));             string dataline = null;             while((dataline = reader.readline()) != null){                 buffer.append(dataline);             }             string responsemsg =    buffer.tostring();             system.out.println(responsemsg);         }     } } 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -