Provide a programming example in which multithreading does not provide better performance than a single-threaded solution
Example below shows, that multi-threading programs not always have performance benefit.
For example simple tasks(like iterating 100 elements) will not give better performance, but will use more CPU and RAM memory for it
Example:
import java.util.Random;
public class Main {
public static void main(String[] args) {
int[] arr = new int[100];
new Thread(() -> {
for (int i = 0; i < arr.length; i++) {
arr[i] = new Random().nextInt();
System.out.println("Thread " + arr[i]);
}
}).start();
}
}