Program: Design of multi-threaded matrix multiplication algorithm
Description: Using multithreading to multiply matrices, because the operations of each thread do not affect each other,
So instead of using locks, the code is as follows:
Thread. Operatematrix class, Implementing matrix operations
1 /*2 * Description: Define Matrix Operation class3 * 4 * Written By:cai5 * 6 * Date written:2017-10-257 * 8 * */9 Ten Packagethread; One A Public classOperatematrix { - - int[] matrix1 =NULL;//First Matrix the int[] matrix2 =NULL;//a second matrix - int[[] result =NULL;//storing matrix multiplication results - Public Static intline = 0;//record the number of lines of the first matrix that are currently participating in the calculation - + //Defining construction Methods - PublicOperatematrix () {} + A PublicOperatematrix (int[] M1,int[] m2) { at - This. matrix1 =M1; - This. matrix2 =m2; -result =New int[Matrix1.length] [Matrix2[0].length]; - } - in //returns the result of multiplying the matrix - Public int[] GetResult () { to + Try { - the /* * * When the matrix is not fully calculated $ * Make the current thread sleep 1 milliseconds waitingPanax Notoginseng * Then judge again - * the * */ + while(Operatematrix.line <matrix1.length) { A theThread.Sleep (1); + - } $}Catch(Exception e) { $ - e.printstacktrace (); - } the - return This. Result;Wuyi the } - Wu //The row of the first matrix is multiplied by the column of the second matrix to get the row of the new matrix - Public voidoperate () { About $Operatematrix.line + = 1;//number of record lines plus 1 - - for(inti = 0; i < matrix1[0].length; i++ ) { - A intsum = 0;//calculates the result of the column that stores the first matrix row and the second matrix + for(intj = 0; J < Matrix2.length; J + + ) { the -Sum + = matrix1[operatematrix.line-1][j] * Matrix2[j][i];//multiply the current row of the first matrix by the second matrix $ } the theResult[operatematrix.line-1][i] = sum;//Save Results the } the - } in}
Thread. Threadoperate class, Thread implementation class
1 /*2 * Description: Define class, Inherit thread class, overwrite run () method3 * 4 * Written By:cai5 * 6 * Date written:2017-10-257 * 8 * */9 Ten Packagethread; One A Public classThreadoperateextendsThread { - - PrivateOperatematrix om =NULL;//defining a Matrix manipulation object the - //Defining construction Methods - Publicthreadoperate () { - Super(); + } - + Publicthreadoperate (Operatematrix om,string name) { A Super(name);//Thread Name at This. Om =om; - } - - //overwrite the Run () method - @Override - Public voidrun () { in - Try { toSystem.out.println (Thread.CurrentThread (). GetName ());//Print the name of the current thread +}Catch(Exception e) { - e.printstacktrace (); the } * $ /*Panax Notoginseng * Call the Operate method of the Operatematrix object to calculate the matrix - * Each call counts only one row of results the * + * */ A This. Om.operate (); the } + -}
Main. Testdemo Test class
1 /*2 * Description: Define test class3 * 4 * Written By:cai5 * 6 * Date written:2017-10-257 * 8 * */9 Ten One Packagemain; A - - Importthread.*; the - Public classTestdemo { - - Public Static voidMain (String args[]) { + - //define two matrices + int[] M1 = {{1,4,1,1},{4,1,1,1},{1,3,3,6},{1,6,9,0}};//4*4 A int[] m2 = {{2,2,2,2},{2,2,2,2},{2,2,2,2},{2,2,2,2}};//4*4 at -Operatematrix om =NewOperatematrix (M1,M2);//instantiating a Operatematrix object - - //start a corresponding number of threads based on the number of rows in the first matrix - for(inti = 0; i < m1.length; i++ ) { - in NewThreadoperate (OM, "computes the first matrix of the" + (i+1) + "Row * All columns of the second matrix"). Start (); - } to + -Display (Om.getresult ());//Print Results the * } $ Panax Notoginseng - //Print calculation results (for convenience, define the printing method in the test class and should not actually do so) the Public Static voidDisplayint[] [] result) { + A for(inti = 0; i < result.length; i++ ) { the + for(intj = 0; J < Result[i].length; J + + ) { - $System.out.print (Result[i][i] + "\ T" ); $ } - - System.out.println (); the } - }Wuyi the}
Java Experimental Project four--multithreading matrix multiplication algorithm Design