import java.io.*; import java.math.*; import java.util.*; public class PrimeHandler extends Thread { private PrimeStorage ps = new PrimeStorage(); private String fileName = "prime_storage"; private String password = "2isAnOddPrime"; public static void main(String[] args) { new PrimeHandler(); } public PrimeHandler() { //setDaemon(true); setPriority(Thread.MIN_PRIORITY); // be nice! start(); } public String getPrime(String password) { if(password.equals(this.password)) return "\nPrime: " + ps.prime + "\nBinary digits: " + ps.digits + "\nFound: " + ps.date; return "Wrong password!"; } public void run() { load(); while(true) { ps.digits++; ps.prime = new BigInteger(ps.digits, 0, new Random()); ps.date = new Date(); save(); try { sleep(5000); } catch(InterruptedException ie) {} // be nice! } } private void save() { try { ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fileName)); oos.writeObject(ps); oos.close(); } catch (IOException ioe) { System.out.println("IOException: " + ioe); } } private void load() { try { ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fileName)); ps = (PrimeStorage)ois.readObject(); ois.close(); } catch (Exception e) { System.out.println("Exception: " + e); } } } class PrimeStorage implements Serializable { public int digits = 1; public BigInteger prime = null; public Date date = null; }