遗憾的是,我对如何开始使用多线程非常感到失望。 Here is my un-threaded sieve code。我假设线程代码看起来非常相似。也许是这样的??? :
public class PrimeThread extends Thread {
private int start, stop;
ArrayList<Integer> list;
public PrimeThread(int start, int stop, ArrayList<Integer> list) {
this.start = start;
this.stop = stop;
this.list = list;
}
public void run() {
boolean done = false;
int currPrime = 0, pos = 2, initVal = 0;
outerloop:
for (int i = start; i < stop; i++) { //for every number in the list
while (list.get(pos) == 0) {
if (pos == list.size() - 1) {
break outerloop;
}
pos++;
}
currPrime = list.get(pos++); //set the current prime
initVal = currPrime; //save the initial val of the prime
done = false;
while (!done) {
try {
//increment the currPrime by its initial value
currPrime += initVal;
//set the number at the new currPrime position to 0
list.set(currPrime, 0);
} catch (IndexOutOfBoundsException e) {
//if currPrime position > list size, break out of while loop
done = true;
}
}
}
}
}
有人可以帮忙解释一下如何开始吗?