Why is a single Thread faster than multithreading in my code?

Why is a single Thread faster than multithreading in my code?

My code was very slow, so I thought i could push it with multithreading. But it got slower when I used multithreading. It is for a convolution operation. The Matrix[] in length is about 1 to 64 and each Matrix is 28x28 big. Also the Matrix[][] kernel length is 1 to 64 in the first dimension and also in the second dimension and each kernel is 3x3 big.(Matrix.matrix is a double[][]-array)

My code was very slow, so I thought i could push it with multithreading. But it got slower when I used multithreading. It is for a convolution operation. The Matrix[] in length is about 1 to 64 and each Matrix is 28x28 big. Also the Matrix[][] kernel length is 1 to 64 in the first dimension and also in the second dimension and each kernel is 3x3 big.(Matrix.matrix is a double[][]-array)

I already tried using Executorservice, but there was the same problem.

public static Matrix[] convolve(Matrix[] in, Matrix[][] kernel) {

// Defining size of output matrix
int kr = kernel[0][0].rows - 1;
int kc = kernel[0][0].cols - 1;

Matrix[] out = new Matrix[kernel.length];

for (int i = 0; i < kernel.length; i++) {
    out[i] = new Matrix(in[0].rows - kr, in[0].cols - kc);
}

// Convolution operation
for (int i = 0; i < out[0].rows; i++) {
    for (int j = 0; j < out[0].cols; j++) {

        double sum = 0;
        for (int m = 0; m < kernel.length; m++) { // Size of filters
            for (int n = 0; n < kernel[m].length; n++) { // Depth of filters
                for (int k = 0; k < kernel[m][n].rows; k++) { // Stride over
                    for (int l = 0; l < kernel[m][n].cols; l++) { // Stride over
                        sum += in[n].matrix[i + k][j + l] * kernel[m][n].matrix[k][l];
                    }
                }
            }
            out[m].matrix[i][j] = sum;
        }

    }
}

return out;

}

public Matrix[] convolveWithThreads(Matrix[] in, Matrix[][] kernel) { // Defining size of output matrix int kr = kernel[0][0].rows - 1; int kc = kernel[0][0].cols - 1;

Matrix[] out = new Matrix[kernel.length];

for (int i = 0; i < kernel.length; i++) {
    out[i] = new Matrix(in[0].rows - kr, in[0].cols - kc);
}
ArrayList<Thread> ar = new ArrayList<>();
// Convolution Operation
for (int t = 0; t < kernel.length; t++) {
    final int m = t;
    Thread th = new Thread(new Runnable() {

        @Override
        public void run() {
            for (int i = 0; i < out[0].rows; i++) {
                for (int j = 0; j < out[0].cols; j++) {

                    double sum = 0;
                    for (int n = 0; n < kernel[m].length; n++) { // Depth of filters
                        for (int k = 0; k < kernel[m][n].rows; k++) { // Stride over
                            for (int l = 0; l < kernel[m][n].cols; l++) { // Stride over
                                sum += in[n].matrix[i + k][j + l] * kernel[m][n].matrix[k][l];
                            }
                        }
                    }
                    out[m].matrix[i][j] = sum;
                }
            }
        }
    });
    th.start();
    ar.add(th);
}

for (Thread t : ar) {
    try {
        t.join();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

return out;

}

}

Without threads it can do 70000 operations in five minutes and with threads it can only do 40000 operations. (Matrix[] in length = 8 and Matrix[][] kernel length = 8 and 8)

java multithreading

What's new in Bootstrap 5 and when Bootstrap 5 release date?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Random Password Generator Online

HTML Color Picker online | HEX Color Picker | RGB Color Picker

Java Core (2020) | Java for beginners | Brush up your Java Skills | Get Hired

In this video we will learn about all the major concepts that come under Java Core . The course is very carefully designed and is made with keeping simplicit...

Java Fundamentals: Learn Java for absolute beginners |Simpliv

Java Fundamentals: Learn Java for absolute beginners

Java Essentials : Learn Core Java From Basic to Advance

Learn Java Programming Using Practical Assignments. Start Building Back-end Web Applications Robust Test Automation Frameworks By End Of The Course. Learn More!

Multithreading In Java Tutorial | Java Tutorial For Beginners

This tutorial on “Java Threads” will talk about one of the core concepts of Java i.e Java Threads. It will give you a complete insight into how to create, work and synchronize with multiple threads.

How Java Became The Best Career Option for IT Jobs in 2020?

Join CETPA, India's best institute for Java online training and Certification Course. Enroll now to learn Core to Advanced Java & avail 50% discount on Java Online Course with Certificate.