Hi ! I want to make a simple math operations on a vector(array) using two cores of my CPU. The program doesn't work correctly. Please explain me how to solve my problem.
public class MyRunnable implements Runnable { private int startIndex; private int endIndex; private float[] tab; public MyRunnable(int startIndex, int endIndex, float[] tab) { this.startIndex = startIndex; this.endIndex = endIndex; this.tab = tab; } @Override public void run() { for(int i = startIndex; i < endIndex; i++) { tab[i] = i * 2; } System.out.println("Finished"); } }
public class Test { public static void main(String[] args) { int size = 10; int n_threads = 2; float tab[] = new float[size]; for(int i = 0; i < size; i++) { tab[i] = i; } for(int i = 0; i < size; i++) { System.out.println(tab[i]); } Runnable r1 = new MyRunnable(0, size / n_threads, tab ); Runnable r2 = new MyRunnable(size / n_threads, size, tab ); Thread t1 = new Thread(r1); Thread t2 = new Thread(r2); t1.start(); t2.start(); for(int i = 0; i < size; i++) { System.out.println(tab[i]); } } }