# 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.rows - 1;
int kc = kernel.cols - 1;

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

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

// Convolution operation
for (int i = 0; i &lt; out.rows; i++) {
for (int j = 0; j &lt; out.cols; j++) {

double sum = 0;
for (int m = 0; m &lt; kernel.length; m++) { // Size of filters
for (int n = 0; n &lt; kernel[m].length; n++) { // Depth of filters
for (int k = 0; k &lt; kernel[m][n].rows; k++) { // Stride over
for (int l = 0; l &lt; 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.rows - 1;
int kc = kernel.cols - 1;
```Matrix[] out = new Matrix[kernel.length];

for (int i = 0; i &lt; kernel.length; i++) {
out[i] = new Matrix(in.rows - kr, in.cols - kc);
}
// Convolution Operation
for (int t = 0; t &lt; kernel.length; t++) {
final int m = t;

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

double sum = 0;
for (int n = 0; n &lt; kernel[m].length; n++) { // Depth of filters
for (int k = 0; k &lt; kernel[m][n].rows; k++) { // Stride over
for (int l = 0; l &lt; 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();
}

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 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.