G
Gelöschtes Mitglied 46335
Gast
Guten Abend community ich steh voll aufm Schlauch was das Thema threads angeht...
ich habe eine Klasse Primes(vorgegeben) und eine Klasse PrimeThread die ich begonnen habe selbst zu schreiben... jetzt muss ich irgendwie in die Klasse Primes noch das, welches ich als Kommentar in der Klasse PrimeThread habe einfügen nur habe ich null Idee wo und wann und wie ... gibts da irgendwie nen kleinen Tipp??
bin über jede Hilfestellung erfreut
schönen Abend noch lg
ich habe eine Klasse Primes(vorgegeben) und eine Klasse PrimeThread die ich begonnen habe selbst zu schreiben... jetzt muss ich irgendwie in die Klasse Primes noch das, welches ich als Kommentar in der Klasse PrimeThread habe einfügen nur habe ich null Idee wo und wann und wie ... gibts da irgendwie nen kleinen Tipp??
Java:
package aufgabe36;
import javax.swing.JOptionPane;
public class Primes {
/**
* Prüft, ob die Zahl n eine Primzahl ist.
* !!! Diese Methode soll nicht verändert werden !!!
*/
public static boolean isPrime(int n) {
if (n < 2) {
return false;
}
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
private static int countPrimes = 0;
/**
* prüft für die Folge der Zahlen von start bis limit im Abstand step, ob
* sie Primzahlen sind. Der Zähler countPrimes wird für jede gefundene
* Primzahl hochgezählt
*
* @param start
* @param step
* @param limit
*/
public static void runTest(int start, int step, int limit) {
for (int current = start; current < limit; current += step) {
if (isPrime(current)) {
countPrimes++;
}
}
}
public static void main(String[] args) {
String inp = JOptionPane.showInputDialog("Limit n:", "1000000");
int maxValue;
if (inp != null) {
maxValue = Integer.parseInt(inp);
} else {
// Abbrechen wurde gewählt
return;
}
System.out.print("Number of primes up to " + maxValue + ": ");
long tstart = System.nanoTime();
if (maxValue >= 2) {
countPrimes = 1; // 2 ist einzige gerade Primzahl
int startValue = 3;
int step = 2;
runTest(startValue, step, maxValue);
}
long tend = System.nanoTime();
System.out.println(countPrimes);
double estimated = maxValue / Math.log(maxValue);
System.out.printf("Estimated number : %8.1f%n", estimated);
double diffPercent = 100 * (countPrimes - estimated) / countPrimes;
System.out.printf("Difference: %5.2f %% %n", diffPercent);
System.out.printf("Runtime: %8.2f msec.%n", (tend - tstart) / 1.0e6);
}
}
Java:
package aufgabe36;
public class PrimeThread implements Runnable{
//for(int i = 0; i < 4; i++){
//PrimeThread pt = new PrimeThread();
//Thread x = new Thread(pt);
//x.start();
//}
@Override
public void run() {
// TODO Auto-generated method stub
}
}
bin über jede Hilfestellung erfreut
schönen Abend noch lg