2014-06-09 58 views
0

下面的代碼似乎不平行地運行,但在發出其他甚至要求在每個單獨的線程AWAIT()方法後一個請求。有人可以幫助使這個線程並行調用。的CyclicBarrier勞克並行線程執行不同的邏輯

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 
} 


} 

回答

2

要初始化與1的CyclicBarrier在步: CyclicBarrier cyclicBarrier = new CyclicBarrier(1, new Runnable()....

您應將其更改爲2,使這一障礙等待2個線程到達,然後它打破。