package untitled1;import java.io.*;
import java.util.*;public class ParallelSol {  public static final String[] MatrixString = {
      "RowNum", "ColNum"};  public static int M = getInt(MatrixString[0]);
  public static int N = getInt(MatrixString[1]);  public int A[][] = new int[M][N];
  public int j;  public void getArray() {    System.out.println("M&iexcl;&Aacute;N dimensional matrix A is following:");    for (int j = 0; j < M; ++j) {
      for (int i = 0; i < N; ++i) {        Random rand = new Random(); // Random integers
        int k = rand.nextInt();
        A[j][i] = k;
        System.out.print(A[j][i] + " ");
      }
      System.out.println("");
    }
  }  public static int getInt(String aMatrixString) {
    int Num;
    String inStr = null;    try {
      BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
      System.out.print("Enter an integer number for " + aMatrixString + ": ");
      inStr = in.readLine();
    }
    catch (IOException ioe) {
      System.out.println(ioe);
    }    Num = Integer.parseInt(inStr);    return Num;
  }
}class ParSol    extends ParallelSol     implements Runnable {  static int m = M;
  static int n = N;
  private int A[][]=new int[m][n];  //zpxocivuby  Add at 2004-1-5
  long b[] = new long[n];           //zpxocivuby  Add at 2004-1-5
  public Thread me=new Thread(this);//zpxocivuby  Modify at 2004-1-5  public void ParSol(int[][] a){    //zpxocivuby  Add at 2004-1-5
    A=a;                            //zpxocivuby  Add at 2004-1-5
  }                                 //zpxocivuby  Add at 2004-1-5  public void join() {    try {
      me.join();
    }
    catch (InterruptedException e) {}
  }  public void run() { //thread    long rowProduct = 0;
    for (int i = 0; i < n; ++i) {
      rowProduct += A[j][i];
    }
    b[j] = rowProduct;  }
  public static void main(String[] args) {  ParallelSol theApp = new ParallelSol();
  theApp.getArray();//start a thread to compute each b[j]
  ParSol[] B = new ParSol[m];
  for (int j = 0; j < m; ++j) {
    B[j] = new ParSol();
    B[j].ParSol(theApp.A);    //zpxocivuby  Add at 2004-1-5
    B[j].me.start();
//wait for them to finish
  }
  for (int j = 0; j < m; ++j) {
    B[j].join();  }
  for (int j = 0; j < B.length;j++){
  System.out.print(B[j].b[0]);   //zpxocivuby  Modify at 2004-1-5
  System.out.print("   ");      //zpxocivuby  Add at 2004-1-5
  }}
}

解决方案 »

  1.   

    我想对你说几点:
    1.该程序是在你的基础上改的,不是我重写的,如果让我来写我决不会写着样的程序
    2.没有构造函数,一个类如果没有构造函数,就如同瞎子一样,你不知道初始化后要做什么东西
    3.你对继承的概念不清楚,我添加了一个方法 ParSol(int[][] a)将你的 ParSol.A初始化,你本想引用父类的A但是这样是不会引用的,只能继承父类的变量和方法,不能直接引用父类的变量,可见你的java类概念很薄弱,建议你重新学习oop。
    4.java的接口概念模糊 runnable是接口只有一个方法run()该方法仅仅实现了thread的run方法,并且只能在扩展该接口的类中运行它也就是我添加的me.start()。
    5.以后注意写程序不要吝啬你的空格、换行和//注释,不要浮躁,要踏踏实实的写程序.切记切记!!
      

  2.   

    zpxocivuby(荒原困兽),为什么我还是运行不通过呢?运行后,输入行数和列数,就提示如下错误:
    Exception in thread "main" java.lang.NoClassDefFoundError: ParallelSol 
      

  3.   

    另外,还是要非常感谢zpxocivuby(荒原困兽)的教诲。不过我刚刚接触JAVA才一个月,正在看书的时间也没有超过2个星期。我想自己还是有很大潜质的吧。^_^谢谢zpxocivuby(荒原困兽)!!!
      

  4.   

    题目别写的那么大,“在多个cpu上运行”,你的程序没有体现出这个概念,改成“多线程”比较合适,除非使用jni,否则java是无法控制到cpu这个层次的。
      

  5.   

    bw78619(Genghis Khan) :
    这个程序本身就是个作业,作业要求如此,我也没有办法。
      

  6.   

    运行前已经去掉package untitled1;