Converter para multithread

1 resposta
tpiardi

Olá, eu preciso desenvolver um código de multiplicação de matrizes usando multithread.
Estou já a algumas horas lendo a teoria, mas não consegui encaixar ainda.

Preciso fazer duas versões além da abaixo,
em uma cada thread calcula uma linha da matriz resultante
e em outra cada thread calcula um elemento da matriz.

Não há necessidade de desenvolver o código para mim, mas se puderem deixar umas dicas e a direção em que olhar, fico muito agradecido, estou meio perdido com essa atividade.

Muito obrigado

public class Matriz {  
  
   public static void main(String[] args) {  
      int x[][] = { { 3, 2, 3 }, { 5, 9, 8 }, };  
      int y[][] = { { 4, 7 }, { 9, 3 }, { 8, 1 }, };  
      int z[][] = Matriz.multiply(x, y);  
      Matriz.mPrint(x);  
      Matriz.mPrint(y);  
      Matriz.mPrint(z);  
   }  
  
   public static int[][] multiply(int[][] mOne, int[][] mTwo) {  
      int mOneRows = mOne.length;  
      int mOneCols = mOne[0].length;  
      int mTwoRows = mTwo.length;  
      int mTwoCols = mTwo[0].length;  
      if (mOneCols != mTwoRows) {  
         throw new IllegalArgumentException("matrizes não correspondem: "+ mOneCols + " diferente de " + mTwoRows);  
      }     
      int[][] result = new int[mOneRows][mTwoCols];  
  
      // multiplica  
      for (int i = 0; i < mOneRows; i++) {  
         for (int j = 0; j < mTwoCols; j++) {  
            for (int k = 0; k < mOneCols; k++) {  
               result[i][j] += mOne[i][k] * mTwo[k][j];  
            }     
         }  
      }     
      return result;  
   }  
  
   /*
    * Imprime a multiplição das matrizes
    */  
   public static void mPrint(int[][] a) {  
      int lines = a.length;  
      int cols = a[0].length;  
      System.out.println("array[" + lines + "][" + cols + "] = {");  
      for (int i = 0; i < lines; i++) {  
         System.out.print("{");  
         for (int j = 0; j < cols; j++) {  
            System.out.print(" " + a[i][j] + ",");  
         }  
         System.out.println("},");  
      }  
      System.out.println(":;");  
   }  
}

1 Resposta

ViniGodoy
Criado 15 de agosto de 2011
Ultima resposta 15 de ago. de 2011
Respostas 1
Participantes 2