If someone could help me, i would appreciate it. I am new to Java and need some help. I need to write a multithreaded java or pthreads program that outputs prime numbers. The user needs to run the program and enter a number on the command line. The program needs to create a seperate thread that outputs all the prime numbers less than or equal to the number that the user entered. Thanks in advance......
//Main Program Primes.java
import java.io.*;
public class Primes {
public static void main(String args[]) {
try {
PrimeThread pt=null;
//read in parameters
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a number> ");
int limit=Integer.parseInt(br.readLine());
System.out.print("Enter a file name to store the results> ");
String fName=br.readLine();
//create PrimeThread
if(fName.length()>0) pt=new PrimeThread(limit, new FileOutputStream(fName)); //output to file
else pt=new PrimeThread(limit); //output to standard output stream
pt.run(); //run the thread
} catch(Exception e) { //implement better exception handling than this
e.printStackTrace();
}
}
}
Code:
//Thread class PrimeThread.java
import java.io.*;
class PrimeThread extends Thread {
private PrintStream pOut=null;
private int limit=0;
//default constructor. does nothing
public PrimeThread() {
}
//constructor to set the number below which to generate primes
//no output stream is specified, so it outputs to the System.out
public PrimeThread(int l) {
limit=l;
try {
pOut=System.out;
} catch(Exception e) {
e.printStackTrace();
}
}
//constructor that sets both the number, as above, and specifies an output stream
//if the specified stream is null, uses System.out
public PrimeThread(int l, OutputStream outS) {
limit=l;
try {
if(outS!=null) {
pOut=new PrintStream(outS);
} else {
pOut=System.out;
}
} catch(Exception e) {
e.printStackTrace();
}
}
//method that performs the work of the thread, in this case the generation of prime numbers.
public void run() {
//compute primes via the seive
boolean numbers[]=new boolean[limit+1];
numbers[0]=false; //need to set 0 not prime
numbers[1]=false; //need to set 1 not prime
for(int i=2; i<numbers.length; i++) { //initially set all other numbers potentially prime
numbers[i]=true;
}
for(int i=2; i<numbers.length; i++) {
if(numbers[i]) { //if this number has been eliminated, so have its multiples
for(int j=(2*i); j<numbers.length; j+=i) { //eliminate multiples of the current number
numbers[j]=false;
}
}
}
for(int i=0; i<numbers.length; i++) { //output the numbers found to be prime
if(numbers[i]) pOut.println(i);
}
}
}
Kids, kids... you tried your best, and you failed miserably; the lesson is: never try.
This sounds like a school assignment, and I've already done waaay too much.
There is another constructor for the PrimeThread class that will not require a file name, and it should be easy enough for you to look through the 25 or so lines of code in Primes.java and remove the part where it asks for a file name.
You can do it! I believe in you! Insert generic inspirational encouragement here!
Kids, kids... you tried your best, and you failed miserably; the lesson is: never try.
Bookmarks