普通矩阵乘法-多线程算法

package com.data.struct;

import java.util.concurrent.CountDownLatch;

public class MultiThreadMatrixMultipy {
	
	static int[][]a=new int[][]{
			{1,2,3,4},
			{5,6,7,8},
			{9,10,11,12},
			{13,14,15,16}
	};
	static int[][]b=new int[][]{
			{1,3,5,7},
			{2,4,6,8},
			{11,13,15,17},
			{12,14,16,18}
	};
	static int[][]c=new int[a.length][a.length];
	
	private static void matrixFirstLoop(int[][]a,int[][]b,int[][]c,int i,int i2,CountDownLatch cdl)throws Exception{
		if(i==i2){
			CountDownLatch cd=new CountDownLatch(2);
			matrixSecondLoop(a,b,c,i,0,b.length-1,cd);
		}else{
			int mid=(i+i2)/2;
			new maxtrixFirstThread(a,b,c,i,mid,cdl).start();
			new maxtrixFirstThread(a,b,c,mid+1,i2,cdl).start();
			cdl.await();
		}
	}
	
	private static void matrixSecondLoop(int[][]a,int[][]b,int[][]c,int i,int j,int j2,CountDownLatch cdl)throws Exception{
		int n=c.length;
		if(j==j2){
			c[i][j]=0;
			for(int k=0;k<n;k++){
				c[i][j]=c[i][j]+a[i][k]*b[k][j];
			}
			
		}else{
			int mid2=(j+j2)/2;
			new maxtrixSecondThread(a,b,c,i,j,mid2,cdl).start();
			new maxtrixSecondThread(a,b,c,i,mid2+1,j2,cdl).start();
			cdl.await();
			
		}
	}
	
	private static class maxtrixFirstThread extends Thread{ 
		private int[][]a;
		private int[][]b;
		private int[][]c;
		private int i;
		private int i2;;
		private   CountDownLatch cdl;
		public maxtrixFirstThread(int[][]a,int[][]b,int[][]c,int i,int i2,CountDownLatch cdl){
			this.a=a;
			this.b=b;
			this.c=c;
			this.i=i;
			this.i2=i2;
			this.cdl=cdl;
		}
		@Override
		public void run() {
			try {
				CountDownLatch cdlnext=new CountDownLatch(2);
				matrixFirstLoop(a,b,c,i,i2, cdlnext);
				cdl.countDown();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	private static class maxtrixSecondThread extends Thread{ 
		private int[][]a;
		private int[][]b;
		private int[][]c;
		private int j;
		private int j2;
		private int i;
		private   CountDownLatch cdl;
		public maxtrixSecondThread(int[][]a,int[][]b,int[][]c,int i,int j,int j2,CountDownLatch cdl){
			this.a=a;
			this.b=b;
			this.c=c;
			this.j=j;
			this.j2=j2;
			this.cdl=cdl;
			this.i=i;
		}
		@Override
		public void run() {
			try {
				CountDownLatch cdlnext=new CountDownLatch(2);
				matrixSecondLoop(a,b,c,i,j,j2, cdlnext);
				cdl.countDown();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	private static int[][] ordernaryMultipy(int [][] a,int[][]b){
		int n=a.length;
		int [][] c=new int[n][n];
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				for(int k=0;k<n;k++){
					c[i][k]+=a[i][j]*b[j][k];
				}
			}
		}
		return c;
	}
	
	public static void printC(){
		for(int i=0;i<c.length;i++){
			for(int j=0;j<c[i].length;j++){
				System.out.print(c[i][j]+" ");
			}
			System.out.println();
		}
	}
	
	
	public static void main(String[] args)throws Exception {
		CountDownLatch cdlnext=new CountDownLatch(2);
		matrixFirstLoop(a,b,c,0,a.length-1,cdlnext);
		cdlnext.await();
		printC();
	}

}

你可能感兴趣的:(普通矩阵乘法-多线程算法)