Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
160
Chapter 7 Generating the Server Response: HTTP Response Headers
Listing 7.3  PrimeList.java 
package coreservlets;
import java.util.*;
import java.math.BigInteger;
/** Creates a Vector of large prime numbers, usually in
 *  a low priority background thread. Provides a few small
 *  thread safe access methods.
*/
public class PrimeList implements Runnable {
  private Vector primesFound;
  private int numPrimes, numDigits;
  /** Finds numPrimes prime numbers, each of which are
   *  numDigits long or longer. You can set it to only
   *  return 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) {
    // Using Vector instead of ArrayList
    // to support JDK 1.1 servlet engines
    primesFound = new Vector(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
      start = Primes.nextPrime(start);
      synchronized(this) {
        primesFound.addElement(start);
      }
    }
  }
  public synchronized boolean isDone() {
    return(primesFound.size() == numPrimes);
  }
Second edition of this book: www.coreservlets.com; Sequel: www.moreservlets.com.
Servlet and JSP training courses by book's author: courses.coreservlets.com.






footer




 

 

 

 

 Home | About Us | Network | Services | Support | FAQ | Control Panel | Order Online | Sitemap | Contact

jsp web hosting

 

Our partners: PHP: Hypertext Preprocessor Best Web Hosting Java Web Hosting Jsp Web Hosting Cheapest Web Hosting

Visionwebhosting.net Business web hosting division of Web Design Plus. All rights reserved