Is it possible to pass the value for i into the new thread object?
Current code:
int cores = 4;
final int n = 10000;
static double sum = 0.0;
Thread[] threads = new Thread[cores];
for (int i = 0; i < cores; i++)
{
threads[i] = new Thread(new Runnable() {
public void run() {
for (int j = i * (n / cores); j < (i + 1) * N / P; j++) {
double x = (j + 0.5) * step;
sum += 4.0 / (1.0 + x * x);
}
}
});
}
Regards
The minimal change of code that would solve your problem is to add a variable like
indexbelow, with thefinalmodifier:Variables with
finalmodifier are guaranteed not to change and therefore can be used in instances of anonymous classes.