public class SieveGenerator{
static int N = 50;
public static void main(String args[]){
int cores = Runtime.getRuntime().availableProcessors();
int f[] = new int[N];
//fill array with 0,1,2...f.length
for(int j=0;j<f.length;j++){
f[j]=j;
}
f[0]=0;f[1]=0;//eliminate these cases
int p=2;
removeNonPrime []t = new removeNonPrime[cores];
for(int i = 0; i < cores; i++){
t[i] = new removeNonPrime(f,p);
}
while(p <= (int)(Math.sqrt(N))){
t[p%cores].start();//problem here because you cannot start a thread which has already started(IllegalThreadStateException)
try{
t[p%cores].join();
}catch(Exception e){}
//get the next prime
p++;
while(p<=(int)(Math.sqrt(N))&&f[p]==0)p++;
}
//count …Run Code Online (Sandbox Code Playgroud) 我IllegalThreadStateException在使用以下代码时遇到异常:我已经启动了这个线程一次(通过使用thread.start())并再次尝试在另一个地方启动它,因此使用以下代码:
thread.interrupt();
thread.start();
Run Code Online (Sandbox Code Playgroud)
但是thread.start()扔了IllegalThreadStateException.
我应该用什么来解决它?