Skip to content

Instantly share code, notes, and snippets.

@eskatos
Created November 25, 2013 21:44
Show Gist options
  • Select an option

  • Save eskatos/7649437 to your computer and use it in GitHub Desktop.

Select an option

Save eskatos/7649437 to your computer and use it in GitHub Desktop.
public class ThreadedPrimeNumbersSeeker
extends Thread
{
private static final int ceiling = 100;
private static final int interval = 1000;
private static final int delay = 100;
public int count = 0;
public int current = 2;
public int[] primes = null;
public static void main( String[] args )
{
System.out.println( "Step, Current int, # primes" );
ThreadedPrimeNumbersSeeker seeker = new ThreadedPrimeNumbersSeeker();
seeker.start();
int seekStep = 0;
while( true )
{
seekStep++;
System.out.println( seekStep + ", " + seeker.current + ", " + seeker.count );
try
{
sleep( interval );
}
catch( InterruptedException e )
{
System.out.println( "Monitor interrupted." );
}
}
}
@Override
public void run()
{
primes = new int[ ceiling ];
while( count < ceiling )
{
current++;
int j = 2;
boolean isPrime = true;
while( j < current / 2 && isPrime )
{
isPrime = current % j > 0;
j++;
}
if( isPrime )
{
count++;
primes[count - 1] = current;
}
try
{
sleep( delay );
}
catch( InterruptedException e )
{
System.out.println( "Runner interrupted." );
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment