如何在4个线程完成后访问arraylist

时间:2014-05-21 12:35:42

标签: java multithreading arraylist

我运行了4个线程,并且ech run增加了全局arraylist的大小。但是在线程完成后我无法访问生成的arraylist。有没有办法访问生成的arraylist ..或者它如何与线程和重新安装数据结构一起使用?

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次调用的方法..我需要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();

    }

1 个答案:

答案 0 :(得分:0)

假设currCDO是全局的,您是否在等待所有线程完成?

您可能需要考虑为每个线程构建输出列表,然后在最后将它们连接起来。这样可以避免锁定。您也可以使用线程安全列表,但这可能会更慢。我没有特别了解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) {
}