自定义的取消标志
示例
public class PrimeGenerator {
private volatile boolean cancelled;
private final List<BigInteger> primes = new ArrayList<>();
public void run() {
BigInteger p = BigInteger.ONE;
while (!cancelled) {
p = p.nextProbablePrime();
synchronized (this) {
primes.add(p);
}
}
}
public void cancel() {
cancelled = true;
}
public synchronized List<BigInteger> get() {
return new ArrayList<>(primes);
}
}最后更新于