I am struggling to understand on java threads work so excuse this rather simple question.
Let's assume I have a program with N threads. Each thread executes the same instructions on a different part of an array of Strings. We invoke the thread through a class with a runnable interface. For the purposes of this example, let say it is something like this:
run() {
while (startStop = loopGetRange() != null) {
countLetters(startStop.start,startStop.stop);
/* start is the beginning cell in the array where the process starts
and stop is the ending cell in the array where the process stops */
}
}
Finally countLetters is just a simple method as follow:
private void countLeters (int start, int stop) {
for (int y = start; <= stop; y++) {
String theWord = globalArray[y];
int z = theWord.length;
System.out.println("For word "+theWord+" there are "+z+" characters");
}
}
Here is my question: Are variables like "theWord" and "Z" local to the thread or are they shared across the thread and are thus subject to possible thread collisions. If the latter, how best to protect these variables.
Thanks for helping a confused person.
Elliott