我这样的实现是不是狂烂!!!!!!!!!!!!!!
我的变量全放到线程中的,这样怎么行呢!但我想不出什么好办法,大家帮我看一下!谢先!public class Application1 {   private static class Th extends Thread {       int x;
       long A[][]=new long[4][4] ,B[][]=new long[4][4],C[][]=new long[4][4];
               long A11[][]=new long[2][2],A12[][]=new long[2][2],A21[][]=new long[2][2];
               long  A22[][]=new long[2][2];
               long C11[][]=new long[2][2],C12[][]=new long[2][2],C21[][]=new long[2][2];
               long  C22[][]=new long[2][2];
               long B11[][]=new long[2][2],B12[][]=new long[2][2],B21[][]=new long[2][2];
              long  B22[][]=new long[2][2];
              long T1[][]=new long[2][2],T2[][]=new long[2][2],T3[][]=new long[2][2];
               long T4[][]=new long[2][2],T5[][]=new long[2][2],T6[][]=new long[2][2];
               long T7[][]=new long[2][2],T8[][]=new long[2][2];           public Th(int x) {
            this.x = x;        }        public void me1(long A[][], long B[][], long C[][])
        { for (int i=0;i<2;i++)
              for (int j=0;j<2;j++)
              {
                     for (int k1=0;k1<2;k1++)
                         C[i][j]=C[i][j]+A[i][k1]*B[k1][j];
              }
        }        public void me2(long A[][], long B[][], long C[][])
        {
            int i,j;
                for (i=0;i<2;i++)
              for (j=0;j<2;j++)
              {
                          C[i][j]=A[i][j]+B[i][j];              }        }        // Find the maximum value in our particular piece of the array        public void run() {
          for(int i=0;i<4;i++)
          for(int j=0;j<4;j++)
            A[i][j]=2+2*i-j;        for(int i=0;i<4;i++)
      for(int j=0;j<4;j++)        B[i][j]=2+3*i-2*j;
    for (int i=0;i<4;i++)
                  for (int j=0;j<4;j++)
                  {                          C[i][j]=0;
                  }
          for (int i=0;i<2;i++)
                  for (int j=0;j<2;j++)
                  {
                          T1[i][j]=T2[i][j]=T3[i][j]=T4[i][j]=0;
                          T5[i][j]=T6[i][j]=T7[i][j]=T8[i][j]=0;
                  }          for (int i=0;i<2;i++)
                  for (int j=0;j<2;j++)
                  {
                          A11[i][j]=A[i][j];
                  }          for (int i=0;i<2;i++)
                  for (int j=2;j<4;j++)
                  {
                          A12[i][j-2]=A[i][j];
                  }      for (int i=2;i<4;i++)
                  for (int j=0;j<2;j++)
                  {
                          A21[i-2][j]=A[i][j];
                  }      for (int i=2;i<4;i++)
                  for (int j=2;j<4;j++)
                  {
                          A22[i-2][j-2]=A[i][j];
                  }
          for (int i=0;i<2;i++)
                  for (int j=0;j<2;j++)
                  {
                          B11[i][j]=B[i][j];
                  }          for (int i=0;i<2;i++)
                  for (int j=2;j<4;j++)
                  {
                          B12[i][j-2]=B[i][j];
                  }      for (int i=2;i<4;i++)
                  for (int j=0;j<2;j++)
                  {
                          B21[i-2][j]=B[i][j];
                  }      for (int i=2;i<4;i++)
                  for (int j=2;j<4;j++)
                  {
                          B22[i-2][j-2]=B[i][j];
                  }          for (int i=0;i<2;i++)
                  for (int j=0;j<2;j++)
                  {
                          C11[i][j]=C[i][j];
                  }          for (int i=0;i<2;i++)
                  for (int j=2;j<4;j++)
                  {
                          C12[i][j-2]=C[i][j];
                  }      for (int i=2;i<4;i++)
                  for (int j=0;j<2;j++)
                  {
                          C21[i-2][j]=C[i][j];
                  }      for (int i=2;i<4;i++)
                  for (int j=2;j<4;j++)
                  {
                          C22[i-2][j-2]=C[i][j];
                  }                 if(x==1)
                 {me1(A11,B11,T1);
        me1(A12,B21,T2);
         me2(T1,T2,C11);
         for (int i=0;i<2;i++)
                 for (int j=0;j<2;j++)
                 {
                   C[i][j] = C11[i][j];
                   System.out.print(C[i][j]+" ");                 }
                 }      if(x==2)
      { me1(A11,B12,T3);
        me1(A12,B22,T4);
         me2(T3,T4,C12);
         for (int i=0;i<2;i++)
                        for (int j=2;j<(2*2);j++)
                        {
                                 C[i][j]=C12[i][j-2];
                                System.out.print(C[i][j]+" ");
                        }}
      if(x==3)
      {me1(A21,B11,T5);
        me1(A22,B21,T6);
        me2(T5,T6,C21);
        for (int i=2;i<(2*2);i++)
                      for (int j=0;j<2;j++)
                          {
                              C[i][j]=C21[i-2][j];
                              System.out.print(C[i][j]+" ");
                          }
}
       if(x==4)
       {me1(A21,B12,T7);
        me1(A22,B22,T8);
        me2(T7,T8,C22);
        for (int i=2;i<(2*2);i++)
                      for (int j=2;j<(2*2);j++)
                          {
                          C[i][j]=C22[i-2][j-2];
                           System.out.print(C[i][j]+" ");
                          }
}
                }
        }
    public static void main(String[] args) {               Th[] t = new Th[4];        for (int i=0; i < 4; i++) {
        t[i] = new Th(i);
       t[i].start();            System.out.println("the i is complet"+i);
        }
        try {
            for (int i=0; i < 4; i++) {
                t[i].join();
                System.out.println("comple"+i);            }
        }
        catch (InterruptedException e) {        }
    }
}

解决方案 »

  1.   

    先不说你的程序如何在单CPU上单线程肯定比多线程要快!建议认真去了解多线程机制
      

  2.   

    我这里是一定要用多线程的,
    我要把两个实现函数用ejb部署,
    从而达到分布的目的,
    我先在一台机子试,再移到几台机子!
      

  3.   

    什么情况下用多线程?
    当你计算需要访问一些紧缺资源,比如说io,可能等待的时候,考虑使用.
    对于你上述的问题,根本没有必要使用。如果你使用了,那么效率会低。就算是在多cpu环境,同样的也低!因为无论如何,就目前的计算机技术,同一个进程的计算不会被分配到不同的cpu。你要使用ejb,实现分布式,你就更加不用考虑多线程了。不管怎么去分布,你的同一个运算的某一次请求,都会在一台机器上执行。不可能给你同时分在多台机器上计算。
    赫赫,也许人家oracle等公司鼓吹的网格计算会解决这个问题。
      

  4.   

    你是不是很迷信ejb呀? 要知道,ejb里根本不能自己对线程操作的...