Common matrix multiplication-Multithreading algorithm _ algorithm

Source: Internet
Author: User
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}, {1
	3,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 Excep
		tion{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.P
			Rint (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 ();
 }

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.