I am trying to start a variable number of threads within a for loop and want to name the threads prime1, prime2, [...].
The PrimeFinderThread Class extends from Thread.
[...]
for (int i = 0; i <= numberThreads; i++) {
PrimeFinderThread ("prime" + i) = new PrimeFinderThread (lowerBoundary, interval);
}
[...]
I am getting the error:
The left-hand side of an assignment must be a variable.
from ("prime" + i)
What is a possible soloution to start X Threads with a seperate name for each?
Try the following:
It sets the name via the
setName-method and then starts each thread via a call tostart.However, it is probably easier to make the constructor of
PrimeFinderThreadtake an extra argument which is thenameand invoke the super class constructor.IMO, an even better approach is to not extend the
Threadclass but rather provide aRunnable, preferably with some kind ofExecutorService- check e.g. the Executors JavaDoc or the concurrency trail from the Oracle website.