B
Bob Dubery
I wonder if some kind person can deconfuse me here. I have objects
running as threads interfering with each other.
Here's my class...
class primeSeek implements Runnable{
private static int found = 0;
private static int[] possibles;
public primeSeek(int[] passedarr){
possibles = passedarr;
}
public void run(){
int howMany = possibles.length;
for(int testLoop=0; testLoop<howMany;testLoop++){
int aCandidate = possibles[testLoop];
int limit = aCandidate/2;
boolean isPrime = true;
for(int aFactor = 2; aFactor <= limit; aFactor++){
if((aCandidate%aFactor) == 0){
/* aFactor is a factor ofaCandidate
and aFactor < aCandidate
and aFactor > 1
thus aCandidate is not prime */
aFactor = limit;
isPrime = false;
}
}
// was our number prime?
if(isPrime){
found ++;
}
}
// how many primes?
System.out.println(found + " Prime numbers found");
}
}
Now the code that sets up the threads (and passes each one a different
array)
// create threads to check for primes
int[] Candidates = new int[100];
for(int i=0;i<100;i++){
Candidates = LotsOfInts;
}
Thread T1 = new Thread(new primeSeek(Candidates));
T1.start();
// for(int i = 0; i < 100000000; i++){
// }
for(int i=0;i<100;i++){
Candidates = LotsOfInts[i+100];
}
Thread T2 = new Thread(new primeSeek(Candidates));
T2.start();
My problem is that T1 and T2 end up checking the same array for prime
numbers, despite my taking care to set up Candidates[] differently for
each thread. I'm guessing this is something to do with the way I have
declared possibles in primeSeek.
See that loop between the creation of the threads? If I remove the
comments then the interference goes away because T1 halts (and I
presume is garbage collected) prior to T2 being created. But this is
an inelegant way of preventing the interference.
What can I do to stop the threads interfering with each other?
Thanks in advance
Bob
running as threads interfering with each other.
Here's my class...
class primeSeek implements Runnable{
private static int found = 0;
private static int[] possibles;
public primeSeek(int[] passedarr){
possibles = passedarr;
}
public void run(){
int howMany = possibles.length;
for(int testLoop=0; testLoop<howMany;testLoop++){
int aCandidate = possibles[testLoop];
int limit = aCandidate/2;
boolean isPrime = true;
for(int aFactor = 2; aFactor <= limit; aFactor++){
if((aCandidate%aFactor) == 0){
/* aFactor is a factor ofaCandidate
and aFactor < aCandidate
and aFactor > 1
thus aCandidate is not prime */
aFactor = limit;
isPrime = false;
}
}
// was our number prime?
if(isPrime){
found ++;
}
}
// how many primes?
System.out.println(found + " Prime numbers found");
}
}
Now the code that sets up the threads (and passes each one a different
array)
// create threads to check for primes
int[] Candidates = new int[100];
for(int i=0;i<100;i++){
Candidates = LotsOfInts;
}
Thread T1 = new Thread(new primeSeek(Candidates));
T1.start();
// for(int i = 0; i < 100000000; i++){
// }
for(int i=0;i<100;i++){
Candidates = LotsOfInts[i+100];
}
Thread T2 = new Thread(new primeSeek(Candidates));
T2.start();
My problem is that T1 and T2 end up checking the same array for prime
numbers, despite my taking care to set up Candidates[] differently for
each thread. I'm guessing this is something to do with the way I have
declared possibles in primeSeek.
See that loop between the creation of the threads? If I remove the
comments then the interference goes away because T1 halts (and I
presume is garbage collected) prior to T2 being created. But this is
an inelegant way of preventing the interference.
What can I do to stop the threads interfering with each other?
Thanks in advance
Bob