📄 primelist.java
字号:
package coreservlets;
import java.util.*;
import java.math.BigInteger;
/** Creates an ArrayList of large prime numbers, usually in
* a low-priority background thread. Provides a few small
* thread-safe access methods.
* <P>
* Taken from Core Servlets and JavaServer Pages 2nd Edition
* from Prentice Hall and Sun Microsystems Press,
* http://www.coreservlets.com/.
* © 2003 Marty Hall; may be freely used or adapted.
*/
public class PrimeList implements Runnable {
private ArrayList primesFound;
private int numPrimes, numDigits;
/** Finds numPrimes prime numbers, each of which is
* numDigits long or longer. You can set it to return
* only when done, or have it return immediately,
* and you can later poll it to see how far it
* has gotten.
*/
public PrimeList(int numPrimes, int numDigits,
boolean runInBackground) {
primesFound = new ArrayList(numPrimes);
this.numPrimes = numPrimes;
this.numDigits = numDigits;
if (runInBackground) {
Thread t = new Thread(this);
// Use low priority so you don't slow down server.
t.setPriority(Thread.MIN_PRIORITY);
t.start();
} else {
run();
}
}
public void run() {
BigInteger start = Primes.random(numDigits);
for(int i=0; i<numPrimes; i++) {
start = Primes.nextPrime(start);
synchronized(this) {
primesFound.add(start);
}
}
}
public synchronized boolean isDone() {
return(primesFound.size() == numPrimes);
}
public synchronized ArrayList getPrimes() {
if (isDone())
return(primesFound);
else
return((ArrayList)primesFound.clone());
}
public int numDigits() {
return(numDigits);
}
public int numPrimes() {
return(numPrimes);
}
public synchronized int numCalculatedPrimes() {
return(primesFound.size());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -