程序代写代做 Java data structure C Multi-Threading on Matrix Multiplication

Multi-Threading on Matrix Multiplication
Goal: use multi-threading programing to make a matrix multiplication application of two
matrices. You can use the matrices in the description below to test your code.
Note: the following sample code is given in Java, but you are free to use any programming language.
Requirement: your program should
1) accept any matrices as inputs
2) if these matrices are not multipliable, your program should report such error
3) the input and output matrices can be in any format in displaying, but easy to
understand.
Given two matrices, A and B, where matrix A contains M rows and K columns and matrix B contains K rows and N columns, the matrix product of A and B is matrix C, where C contains M rows and N columns. The entry in matrix C for row i, column j (C i , j) is the sum of the products of the elements for row i in matrix A and column j in matrix B. That is,
For example, if A is a 3-by-2 matrix and B is a 2-by-3 matrix, element C3,1 is the sum of A3,1 ¡Á B1,1 and A3,2 ¡Á B2,1.
For this project, you need to calculate each element Ci , j in a separate worker thread. This will involve creating M ¡Á N worker threads. The main¡ªor parent¡ªthread will initialize the matrices A and B and allocate sufficient memory for matrix C, which will hold the product of matrices A and B. These matrices will be declared as global data so that each worker thread has access to A, B, and C.
The parent thread will create M ¡Á N worker threads, passing each worker the values of row i and column j that it is to use in calculating the matrix product. This requires passing two parameters to each thread. The easiest approach with is to create a data structure using a struct. The members of this structure are i and j, and the structure appears as follows:

One approach is for the main thread to create and initialize the matrices A, B, and C. This main thread will then create the worker threads, passing the three matrices¡ª along with row i and column j ¡ªto the constructor for each worker. Thus, the outline of a worker thread appears in the figure below:
Waiting for Threads to Complete
Once all worker threads have completed, the main thread will output the product contained in matrix C. This requires the main thread to wait for all worker threads to finish before it can output the value of the matrix product. Several different strategies can be used to enable a thread to wait for other threads to finish. A simple strategy for waiting on several threads using Java¡¯s join() is to enclose the join operation within a simple for loop. For example, you could join on ten threads as below: