Java实验项目四——多线程矩阵相乘算法的设计

Program:多线程矩阵相乘算法的设计

Description:利用多线程实现矩阵相乘,因为各个线程的运算互不影响,

      所以不用使用锁,代码如下:

thread.OperateMatrix类,实现矩阵运算

 1 /*
 2  * Description:定义矩阵操作类
 3  * 
 4  * Written By :Cai
 5  * 
 6  * Date Written:2017-10-25
 7  * 
 8  * */
 9 
10 package thread;
11 
12 public class OperateMatrix {
13 
14     int[][] matrix1 = null;        //第一个矩阵
15     int[][] matrix2 = null;        //第二个矩阵
16     int[][] result = null;        //存放矩阵相乘结果
17     public static int line = 0;    //记录当前参与计算的是第一个矩阵的第几行
18     
19     //定义构造方法
20     public OperateMatrix() {}
21     
22     public OperateMatrix(int[][] m1,int[][] m2) {
23         
24         this.matrix1 = m1;
25         this.matrix2 = m2;
26         result = new int[matrix1.length][matrix2[0].length];
27     }
28     
29     //返回矩阵相乘的结果
30     public int[][] getResult() {
31         
32         try {
33             
34             /*
35              * 当矩阵还没有完全计算完时
36              * 令当前线程睡眠1毫秒等待
37              * 然后再次判断
38              * 
39              * */
40             while( OperateMatrix.line < matrix1.length ) {
41                 
42                 Thread.sleep(1);
43                 
44             }
45         }catch(Exception e) {
46             
47             e.printStackTrace();
48         }
49         
50         return this.result;
51         
52     }
53     
54     //第一个矩阵的行乘以第二个矩阵的列,得到新矩阵的行
55     public void operate() {
56         
57         OperateMatrix.line += 1;            //记录行数加1
58         
59         for( int i = 0; i < matrix1[0].length; i++ ) {
60             
61             int sum = 0;        //存储第一个矩阵的行和 第二个矩阵的列的计算结果
62             for( int j = 0; j < matrix2.length; j++ ) {
63                 
64                 sum += matrix1[OperateMatrix.line - 1][j] * matrix2[j][i];        //第一个矩阵的当前行乘以第二个矩阵
65             }
66             
67             result[OperateMatrix.line - 1][i] = sum;        //保存结果
68         }
69     
70     }
71 }

thread.ThreadOperate类,线程实现类

 1 /*
 2  * Description:定义类,继承Thread类,覆写run()方法
 3  * 
 4  * Written By:Cai
 5  * 
 6  * Date Written:2017-10-25
 7  * 
 8  * */
 9 
10 package thread;
11 
12 public class ThreadOperate extends Thread {
13 
14     private OperateMatrix om = null;        //定义矩阵操类对象
15     
16     //定义构造方法
17     public ThreadOperate() {
18         super();
19     }
20     
21     public ThreadOperate( OperateMatrix om,String name) {
22         super(name);        //线程名字
23         this.om = om;
24     }
25     
26     //覆写run()方法
27     @Override
28     public void run() {
29         
30         try {
31             System.out.println( Thread.currentThread().getName() );        //打印当前线程的名字
32         }catch( Exception e ) {
33             e.printStackTrace();
34         }
35         
36         /*
37          * 调用OperateMatrix对象的operate方法,进行矩阵的计算
38          * 每次调用只计算一行结果
39          * 
40          * */
41         this.om.operate();        
42     }
43     
44 }

main.TestDemo测试类

 1 /*
 2  * Description:定义测试类
 3  * 
 4  * Written By :Cai
 5  * 
 6  * Date Written:2017-10-25
 7  * 
 8  * */
 9 
10 
11 package main;
12 
13 
14 import thread.*;
15 
16 public class TestDemo {
17 
18     public static void main(String args[]) {
19         
20         //定义两个矩阵
21         int[][] m1 = {{1,4,1,1},{4,1,1,1},{1,3,3,6},{1,6,9,0}};        //4*4
22         int[][] m2 = {{2,2,2,2},{2,2,2,2},{2,2,2,2},{2,2,2,2}};        //4*4
23         
24         OperateMatrix om = new OperateMatrix(m1,m2);                //实例化OperateMatrix对象
25         
26         //根据第一个矩阵的行数,启动对应数量的线程
27         for( int i = 0; i < m1.length; i++ ) {
28             
29             new ThreadOperate( om,"计算第一个矩阵的第" + (i+1) + "行*第二个矩阵的所有列" ).start();
30         }
31         
32 
33         display(om.getResult());    //打印结果
34         
35     }
36     
37     
38     //打印计算结果(为了方便,将打印方法定义在测试类中,实际不应该这样做)
39     public static void display(int[][] result) {
40         
41         for( int i = 0; i < result.length; i++ ) {
42             
43             for( int j = 0; j < result[i].length; j++ ) {
44                 
45                 System.out.print( result[i][i] + "	" );
46             }
47             
48             System.out.println();
49         }
50     }
51     
52 }
原文地址:https://www.cnblogs.com/caizhen/p/7751538.html