public class CalculatePrimes extends Thread {
public static final int MAX_PRIMES = 1000000;
public static final int TEN_SECONDS = 10000;
public volatile boolean finished = false;
public void run() {
int[] primes = new int[MAX_PRIMES];
int count = 0;
for (int i=2; count<MAX_PRIMES; i++) {
//  Check to see if the timer has expired
if (finished) {
break;
}
boolean prime = true;
for (int j=0; j<count; j++) {
if (i % primes[j] == 0) {
prime = false;
break;
}
}
if (prime) {
primes[count++] = i;
System.out.println("Found prime: " + i);
}
}
}
public static void main(String[] args) throws InterruptedException {
CalculatePrimes calculator = new CalculatePrimes();
calculator.start();
try {
//calculator.wait(10000);
Thread.sleep(TEN_SECONDS);
//问题在这儿,sleep了10秒钟,但是怎么线程中的print还是在一直打印呢,这个Thread不是指
//主线程吧,因为我把这句替换成calculator.sleep(TEN_SECONDS);还是同样的效果
//还有我用wait函数还是一样执行print
}
catch (InterruptedException e) {
}
calculator.finished = true;}
}