The code below does not seem to be running in parallel but issuing one request after the other even calling await() method on each individual thread. Can somebody help with making this thread calls in parallel.
public class XYZ {
private static String baseUrl = "http://xyz.polls.com";
public static void main(String[] args) {
MultiThreadedHttpConnectionManager conMgr =new MultiThreadedHttpConnectionManager();
HostConfiguration hostConf = new HostConfiguration();
hostConf.setHost("xyz.polls.com");
HttpConnectionManagerParams connParam = new HttpConnectionManagerParams();
connParam.setMaxConnectionsPerHost(hostConf, 5);
connParam.setMaxTotalConnections(5);
conMgr.setParams(connParam);
HttpClient client = new HttpClient(conMgr);
client.setHostConfiguration(hostConf);
CyclicBarrier cyclicBarrier = new CyclicBarrier(1, new Runnable() {
private int count = 1;
public void run() {
System.out.printf("Cyclic Barrier Finished %d\n", count++);
}
});
System.out.println("Spawning Threads");
for(int i = 0; i < 1; i++){
Thread t1 = new Thread(new UpdateProfile(cyclicBarrier, client));
t1.start();
Thread t2 = new Thread(new UpdateAccount(cyclicBarrier, client));
t2.start();
}
System.out.println("Spawning Finished");
}
private static class UpdateAccountThread implements Runnable{
private CyclicBarrier cyclicBarrier;
private HttpClient httpClient;
public UpdateAccountThread(CyclicBarrier cyclicBarrier, HttpClient httpClient){
this.cyclicBarrier = cyclicBarrier;
this.httpClient = httpClient;
}
/* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
@Override
public void run() {
try{
int count = cyclicBarrier.await();
System.out.println("UpdateAccountThread : Cyclic Barrier count on " +count);
updateAccount(httpClient);
}catch (BrokenBarrierException e) {
e.printStackTrace();
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private static class UpdateProfileThread implements Runnable{
private CyclicBarrier cyclicBarrier;
private HttpClient httpClient;
public UpdateProfileThread(CyclicBarrier cyclicBarrier, HttpClient httpClient){
this.cyclicBarrier = cyclicBarrier;
this.httpClient = httpClient;
}
/* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
@Override
public void run() {
try{
int count = cyclicBarrier.await();
System.out.println("UpdateProfileThread : Cyclic Barrier count on " +count);
updateProfile(httpClient);
}catch (BrokenBarrierException e) {
e.printStackTrace();
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private static void updateProfile(HttpClient client){
// logic here
}
private static void updateAccount(HttpClient client){
// logic here
}
}
You are initializing the CyclicBarrier with 1 in your step:
CyclicBarrier cyclicBarrier = new CyclicBarrier(1, new Runnable()....
You should change it to 2 so that this barrier waits for 2 threads to reach and then it breaks.