java 多线程并行计算之矩阵乘法继承Thread类实现(星星笔记)

24 篇文章 2 订阅
14 篇文章 0 订阅

用java编写两个n阶的方阵A和B的相乘程序,结果存放在方阵C中,继承Thread类实现矩阵的乘法

方阵A和B的初始值如下:(同时开两个线程)

输出:c中所有元素之和、程序的执行时间

具体的程序如下:

package com.xing.matrix;

/**
 * 
 * @author LiuYinxing
 *
 */

public class MatrixMultiplication extends Thread{
	
    private long A[][];  
    private long B[][];  
    private long C[][];  
    private int start ;  
    private int end ;  
    private long sum = 0 ; 
    
   public MatrixMultiplication(long a[][],long b[][],int st,int en){  
      this.A = a;  
      this.B = b;  
      this.start = st;  
      this.end = en;  
      this.C = new long[en][en];  
    } 
   public void run(){
	   int i,j,k;
       for( i=start; i< end; i += 2)    
       {    
           for( j=0;j<end;j++)    
           {    
               C [i][j] = 0;    
               for( k=0; k< end;k++)    
               {    
                   C[i][j]+=A[i][k]*B[k][j];    
               }    
           }    
       }
       for( i=start; i<end; i+=2)    
           for( j=0; j<end; j++)     
               sum += C[i][j]; 
   }
   
   public void serial() {  //串行计算一个矩阵的乘法然后在求和  
       int i,j,k;  
       for( i=0; i< end; i ++)    
       {    
           for( j=0;j<end;j++)    
           {    
               C[i][j]=0;    
               for( k=0; k< end;k++)    
               {    
                   C[i][j]+=A[i][k]*B[k][j];    
               }    
           }    
       }    
       for( i=0; i<end; i++)    
           for( j=0; j<end; j++)     
               sum += C[i][j];    
   }
   public long getSum() {  
       return this.sum;  
   }  
	public static void main(String[] args) throws InterruptedException {
		// TODO Auto-generated method stub
        int i,j,t = 0;  
        int leng = 1500;  
        long startTime,endTime;  
        long a[][] = new long[leng][leng];  
        long b[][] = new long[leng][leng];  
        //--------------对矩阵A和矩阵B进行初始化-------------    
        for(i=0;i<leng;i++)       
        {    
            t=i+1;    
            for(j=0;j<leng;j++)    
            {    
                a[i][j]=t++;    
                b[i][j]=1;    
            }    
        }
        MatrixMultiplication thread1 = new MatrixMultiplication(a, b, 0, leng);  
        MatrixMultiplication thread2 = new MatrixMultiplication(a, b, 1, leng);  
        //-------------------并行计算--------------------
        startTime = System.currentTimeMillis();  
        thread1.start();  
        thread2.start();  
        thread1.join();  
        thread2.join();  
        endTime = System.currentTimeMillis();  
        System.out.println("并行结果 = " + (thread1.getSum() + thread2.getSum()));  
        System.out.println("并行时间 = " + (endTime - startTime));  
      //-------------------串行计算-------------------- 
        startTime = System.currentTimeMillis();  
        MatrixMultiplication Serial = new MatrixMultiplication(a, b, 0, leng);  
        Serial.serial();  
        endTime = System.currentTimeMillis();  
        System.out.println("串行结果 = " + Serial.getSum());  
        System.out.println("串行时间 = " + (endTime - startTime)); 
	}

}
运行结果如下:(其中利用多核并行的部分为乘法和求和)

相对加速比为:58371/30401= 1.920035525147199

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值