简体   繁体   中英

All java threads are running on a single core, ultimately taking too much time to execute

This is my homework problem: I have to do matrix multiplication. My code should create a thread to each element in the resultant matrix. ie, if resultant matrix is mXn size then there should be m*n threads.

( http://iit.qau.edu.pk/books/OS_8th_Edition.pdf page 179)

Yes I finished it by my own. Which is executing fine with all my test cases. Unfortunately, I end up getting 70% credit :( This is my code and test cases.

Matrix Multiplication.zip

When I met my professor regarding my marks. He told me that my code taking too long to execute larger size matrix.

I argued with him that it is a expected behavior as it is obvious that large size data takes more time. However, he disagree with me.

I attached my code and test cases . My code is taking 3 hours. As per my professor it should only take 5 min to execute.

I tried to figured out for last couple of days but I couldn't find exact solution :(

outline of my code

ExecutorService executor = Executors
                .newFixedThreadPool(threadCount); // creating thread pool
                                                    // with fixed threads
        int mRRowLen = matrix1.length; // m result rows length
        int mRColLen = matrix2[0].length; // m result columns length
        mResult = new long[mRRowLen][mRColLen];
        for (int i = 0; i < mRRowLen; i++) { // rows from m1
            for (int j = 0; j < mRColLen; j++) { // columns from m2
                Runnable r = new MatrixMultipliactionThread(matrix1ColLen, i, j, matrix1,
                        matrix2);
                executor.execute(r);
            }
        }
        executor.shutdown();
        while (!executor.isTerminated()) {
            // wait untill it get shutdown 
        }

Run method :

public void run() {

        for (int k = 0; k < m1ColLength; k++) { // columns from m1
            Matrix.mResult[i][j] += matrix1[i][k] * matrix2[k][j];
    }

Thanks in Advance

Ok, I downloaded your zip and ran the program. Your problem isn't in the matrix multiplication at all. The advice in the comments is still valid about reducing the number of threads, however as it stands the multiplication happens very quickly.

The actual problem is in your writeToAFile method - all the single-threaded CPU utilization you are seeing is actually happening in there, after the multiplication is already complete.

The way you're appending your strings:

fileOutputString = fileOutputString + resultMatrix[i][j]

creates thousands of new String objects which then immediately become garbage; this is very inefficient. You should be using a StringBuilder instead, something like this:

StringBuilder sb=new StringBuilder();
for (int i = 0; i < resultMatrix.length; i++) {
    for (int j = 0; j < resultMatrix[i].length; j++) {
        sb.append(resultMatrix[i][j]);
        if (j != resultMatrix[i].length - 1) sb.append(",");
    }
    sb.append("\n");
}

String fileOutputString=sb.toString();

That code executes in a fraction of a second.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM