简体   繁体   English

所有Java线程都在单个内核上运行,最终花费太多时间来执行

[英]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. 也就是说,如果结果矩阵的大小为mXn,则应该有m * n个线程。

( http://iit.qau.edu.pk/books/OS_8th_Edition.pdf page 179) http://iit.qau.edu.pk/books/OS_8th_Edition.pdf第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. 不幸的是,我最终获得了70%的信用:(这是我的代码和测试用例。

Matrix Multiplication.zip 矩阵乘法.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. 我的代码需要3个小时。 As per my professor it should only take 5 min to execute. 根据我的教授,只需5分钟即可执行。

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. 好的,我下载了您的zip并运行了该程序。 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. 实际的问题出在您的writeToAFile方法中-乘法完成后,您看到的所有单线程CPU利用率实际上都发生在其中。

The way you're appending your strings: 追加字符串的方式:

fileOutputString = fileOutputString + resultMatrix[i][j]

creates thousands of new String objects which then immediately become garbage; 创建数千个新的String对象,这些对象立即变为垃圾; this is very inefficient. 这是非常低效的。 You should be using a StringBuilder instead, something like this: 您应该改用StringBuilder ,如下所示:

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. 该代码在不到一秒钟的时间内执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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