public class IsPrime {
	public static void main(String[] args) {
		PrimitivePrimeTest ppt;
		StatusInformation myInfo = new StatusInformation();
		myInfo.setDaemon(true); //declare as daemon thread
		myInfo.setPriority( (Thread.NORM_PRIORITY + 2) <= Thread.MAX_PRIORITY ? Thread.NORM_PRIORITY + 2 : Thread.MAX_PRIORITY );
		myInfo.start();

		ThreadGroup workerThreads	= new ThreadGroup("worker threads");

		for (int i = Integer.parseInt(args[0]); i <= Integer.parseInt(args[1]); i++) {
			ppt = new PrimitivePrimeTest(workerThreads, i, "calculating " +i );
			ppt.start();
		} //for

	} //main
} //class IsPrime2

class PrimitivePrimeTest extends Thread {
	protected int numberToTest;
	boolean earlyExit=false;

	public PrimitivePrimeTest(ThreadGroup tg, int numberToTest, String threadName) {
		super (tg, threadName); //call to super's class constructor
		this.numberToTest = numberToTest;
	} //constructor

	public void run() {
		if (numberToTest == 1)
			earlyExit = true;
		for (int i=2; i < ((int) Math.sqrt(numberToTest))+1; i++) {
			try {
				Thread.sleep(1000);
			} catch (InterruptedException ie) {
				//ignore it
			} //catch
			if (numberToTest % i == 0) {
				earlyExit=true;
				break;
			} //endif
		} //for
		if  (earlyExit != true)
			System.out.println(numberToTest+" is prime");
	} //run()
} //class PrimitivePrimeTest

class StatusInformation extends Thread {
	public void run() {
		while (true) {
			System.out.println("no of currently running threads: "+Thread.activeCount() );
			try {
				Thread.sleep(500);
			} catch (InterruptedException ie) {
				//ignore it
			} //catch
		} //while
	} //run()
} //class StatusInformation