尝试在一个方法中运行4个线程

时间:2014-05-20 20:33:13

标签: java multithreading

我尝试使用代码在4个并行线程上运行,但它不起作用..而且我不知道是否可以这样做。 Lokk在这里:

List<MyObject> head = a.subList(0, 2000);
    List<MyObject> body = a.subList(2000, 5000);
    List<MyObject> body2 =  a.subList(5000, 8000);
    List<MyObject> tail = a.subList(8000, a.size());



  //  List<Integer> tail = numbers.subList(4, 8);

    for (int i = 0; i < head.size(); i++) {
        a = head.get(i);
       for (int j = 0; j < l.size(); j++) {
            b = l.get(j);

            fun1 = curr1;
            fun2 = b.getName();

            int d = dL.execute(fun1,fun2);

            if(d<5){
                int x=a.getName().length();
                int y=b.getName().length();

                if(x<y){
                    currCDO.add(a);

                }
                if(y<x){
                    a.setName(b.getName());
                    currCDO.add(a);
                }
                if(x==y){
                    currCDO.add(a);
                }
                break;}}}


    for (int i = 0; i < body.size(); i++) {
        a = body.get(i);
       for (int j = 0; j < l.size(); j++) {
            b = l.get(j);

            fun1 = curr1;
            fun2 = b.getName();

            int d = dL.execute(fun1,fun2);

            if(d<5){
                int x=a.getName().length();
                int y=b.getName().length();

                if(x<y){
                    currCDO.add(a);

                }
                if(y<x){
                    a.setName(b.getName());
                    currCDO.add(a);
                }
                if(x==y){
                    currCDO.add(a);
                }
                break;}}}       

    for (int i = 0; i < body2.size(); i++) {
        a = body2.get(i);
       for (int j = 0; j < l.size(); j++) {
            b = l.get(j);

            fun1 = curr1;
            fun2 = b.getName();

            int d = dL.execute(fun1,fun2);

            if(d<5){
                int x=a.getName().length();
                int y=b.getName().length();

                if(x<y){
                    currCDO.add(a);

                }
                if(y<x){
                    a.setName(b.getName());
                    currCDO.add(a);
                }
                if(x==y){
                    currCDO.add(a);
                }
                break;}}}

    for (int i = 0; i < tail.size(); i++) {
        a = head.get(i);
       for (int j = 0; j < l.size(); j++) {
            b = l.get(j);

            fun1 = curr1;
            fun2 = b.getName();

            int d = dL.execute(fun1,fun2);

            if(d<5){
                int x=a.getName().length();
                int y=b.getName().length();

                if(x<y){
                    currCDO.add(a);

                }
                if(y<x){
                    a.setName(b.getName());
                    currCDO.add(a);
                }
                if(x==y){
                    currCDO.add(a);
                }
                break;}}}

我想要的是在4个不同的线程中运行这4个双循环来并行运行...是否可能..如果是如何管理这个。谢谢

2 个答案:

答案 0 :(得分:3)

您的4个代码块看起来相同,因此第一步是将算法提取到方法

public void algo(List<MyObject> head) {
    for (int i = 0; i < head.size(); i++) {
        a = head.get(i);
       for (int j = 0; j < l.size(); j++) {
            b = l.get(j);

            fun1 = curr1;
            fun2 = b.getName();

            int d = dL.execute(fun1,fun2);

            if(d<5){
                int x=a.getName().length();
                int y=b.getName().length();

                if(x<y){
                    currCDO.add(a);

                }
                if(y<x){
                    a.setName(b.getName());
                    currCDO.add(a);
                }
                if(x==y){
                    currCDO.add(a);
                }
                break;
           }
        }  
    }  
}

然后一个可能的解决方案是为每个列表调用它:

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

   });
   thread.start();
}

答案 1 :(得分:0)

您需要将循环放入扩展类MyThread的run()方法中,并创建4个MyThread实例并启动它们。

public class MyThread extends Thread{
      @Override
      public void run(){
          //your code to implement the contents of run() method
      }
 }



public class Main{
    public static void main(String[]args){
        for(int i=0; i<4; i++){
             new MyThread().start()
        }
    }
}