2014-05-21 23 views
-4

我有4個線程正在運行,並且運行增加了全局數組列表的大小。但是在線程完成後無法訪問所得到的數組列表。有沒有一種方法可以訪問所得到的數組列表,或者它如何與線程一起工作並回收數據結構?如何在4個線程完成後訪問數組列表

List<MyObject> head = a.subList(0, 2000); 
    List<MyObject> body = a.subList(2001, 5000); 
    List<MyObject> body2 = a.subList(5001, 8000); 
    List<MyObject> tail = a.subList(8001, a.size()); 
    //System.out.println(tail.size()); 
    createAndRunFirst(head); 
    createAndRunFirst(body); 

這些都是調用

,這是被稱爲4 times..and我需要的currCDO arrylist

public void algo(List<MyObject>list){ 
    MyObject a = null; 
    MyObject b = null; 

    int e=0; 
    String curr1=""; 
    String curr2=""; 
    for (int i = 0; i < list.size(); i++) { 
     a =list.get(i); 
     curr1= a.getStreetName(); 
     if(curr1.contains("-")){ 
      curr1=curr1.replace("-", " "); 
     } 

     if(curr1.contains("STRASSE")){ 
      curr1=curr1.replace("STRASSE", "STR."); 
     } 

     else{ 
      curr1=a.getStreetName(); 
     } 

     for (int j = 0; j < lotse.size(); j++) { 
      b = lotse.get(j); 

      curr2=b.getStreetName(); 
      if(curr2.contains("-")){ 
       curr2=curr2.replace("-", " "); 
      } 

      if(curr2.contains("STRASSE")){ 
       curr2=curr2.replace("STRASSE", "STR."); 
      } 

      else{ 
       curr2=b.getStreetName(); 
      } 



      int d = dL.execute(curr1,curr2); 

      if(curr1.length()==curr2.length()){ 
       e=0; 
      } 
      if(curr1.length()< curr2.length()){ 
       e=(curr2.length()*30)/100; 
       //System.out.println(d); 
      } 
      if(curr1.length()> curr2.length()){ 
       e=(curr1.length()*30)/100; 
       //System.out.println(d); 
      } 

      if(d<e && a.getPcode().contains(b.getPcode())){ 
       int x=a.getInstituteName().length(); 
       int y=b.getInstituteName().length(); 

       if(x<y){ 

        currCDO.add(a); 


       } 
       if(y<x){ 

        currCDO.add(a); 

       if(x==y){ 

        currCDO.add(a); 

       } 
       break;}else{ 

        //System.out.println(a.getInstituteName()+"******"); 
        restCDO.add(a); 
       } 
      }//System.out.println(currCDO.size() +"*****"); 
     } 
     } 

} 

public void createAndRunFirst(final List<CrawlerDataObject> list) { 
     Thread thread = new Thread(new Runnable() { 
      @Override 
      public void run(){ 
       algo(list); 

      } 

     }); 
     thread.start(); 

    } 
+0

請添加一些代碼?你有什麼嘗試?我們不知道如何解決你的問題,如果我們甚至不知道什麼代碼導致它 – DankMemes

+0

換句話說,...你說,「我不能訪問由此產生的數組列表。你如何知道你無法訪問它?你的代碼試圖訪問它的地方在哪裏?你期望它做什麼(最好是,你期望它打印什麼)?它實際上做了什麼/打印呢? –

+0

subList創建淺表副本,因此對每個子列表中的對象所做的更改應顯示爲主列表 – DankMemes

回答

0

假設currCDO是全球性的方法,你等待所有線程完成?

您可能需要考慮爲每個線程構建輸出列表,然後在最後對它們進行concactenating。這將避免鎖定。你也可以使用線程安全列表,但這可能會變慢。我不明確c#,但我認爲有線程安全列表。

int i0 = 0; 
int c = a.size()/cThread; 
new List<MyObject>()[cThread]; 
for (int iThread=0 ; iThread<cThread ; ++iThread, i0+=c) { 
    createAndRunFirst(a.subList(i0, i0 + c - 1), aOut[iThread]); 
} 

waitForAllThreads(); // i don't know the c# for this 

for (int iThread=0 ; iThread<cThread ; ++iThread) { 
    currCDO += aOut[iThread]; 
} 


public void createAndRunFirst(final List<CrawlerDataObject> list, List<CrawlerDataObject> out) { 
    Thread thread = new Thread(new Runnable() { 
     @Override 
     public void run(){ 
      algo(list, out); 

     } 

    }); 

public void algo(List<MyObject> list, List<MyObject> currCDO) { 
}