Find the nth Prime Number; Exceptions, Threads JAVA example

public class PrimeThreads {
public static void main( String[] arguments ) {
PrimeThreads pt = new PrimeThreads( arguments );
}

public PrimeThreads( String[] arguments ) {
PrimeFinder[] finder = new PrimeFinder[arguments.length];
for( int i = 0; i < arguments.length; i++ ) {
try {
long count = Long.parseLong( arguments[i] );
finder[i] = new PrimeFinder(count);
System.out.println( “Looking for prime “ + count );
} catch ( NumberFormatException nfe ) {
System.out.println( “Error: “ + nfe.getMessage() );
}
}
boolean complete = false;
while( !complete ) {
complete = true;
for( int j=0;  j < finder.length; j++ ) {
if( finder[j]==null ) continue;
if( !finder[j].finished ) {
complete = false;
} else {
displayResult( finder[j] );
finder[j] = null;
}
}
try {
Thread.sleep( 1000 );
} catch ( InterruptedException ie ) {
// do nothing
}
}
}

private void displayResult( PrimeFinder finder ) {
System.out.println( “Prime “ + finder.target + ” is “ + finder.prime );
}
}

 

public class PrimeFinder implements Runnable {
public long target;
public long prime;
public boolean finished = false;
private Thread runner;

PrimeFinder( long inTarget ) {
target = inTarget;
if( runner == null ) {
runner = new Thread( this );
runner.start();
}
}

public void run() {
long numPrimes = 0;
long candidate = 2;
while( numPrimes < target ) {
if( isPrime(candidate) ) {
numPrimes++;
prime = candidate;
}
candidate++;
}
finished = true;
}

boolean isPrime( long checkNumber ) {
double root = Math.sqrt( checkNumber );
for( int i = 2; i <= root; i++ ) {
if( checkNumber % i == 0 )
return false;
}
return true;
}
}

run with command-line arguments, example: 1 10 100 1000

Example from: Sams Teach Yourself Java in 21 Days; 6th Edition Covers Java 7 and Android; Rogers Cadenhead, copyright 2013; chapter 7

Leave a Reply

You must be logged in to post a comment.