Witam,
Próbowałem rozwiązać zadanie z ProjectEuler przy wykorzystaniu ExecutorService, Callable etc.
Program pokazuje prawidłowy wynik jednak program wielowątkowy trwa 10 razy dłużej niż program wykonujący się w jednym wątku...
Czy może ktoś mógłby mi to wyjaśnić ?
Poniżej zamieszczam klasy składające się na program wielowątkowy i klasa jednowątkowa:
//Collatz.java
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Collatz {
final int MAX_NUMBER = 1000000;
int max = 0, step, maxStep;
double temp;
long start,stop;
ExecutorService exec = Executors.newCachedThreadPool();
FutureColatzCallback<String> ft;
Callable<String> task = new Callable<String>() {
public String call() throws Exception {
start = System.currentTimeMillis();
for(int i = 1; i <= MAX_NUMBER; i++) {
temp = i;
step = 0;
while( temp > 1) {
++step;
if ((temp % 2) == 0)
temp = temp/2;
else
temp = 3*temp+1;
if(step > maxStep ) {
max = i;
maxStep = step;
}
}
}
stop = System.currentTimeMillis();
String returnMessage = "Liczba to: "+max+ " ilość kroków " + maxStep + " wynik znaleziony w " + (stop - start) + "mils";
return returnMessage;
}
};
Collatz() {
ft = new FutureColatzCallback<String>(task);
exec.execute(ft);
}
}
//FutureColatzCallback.java
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
import javax.swing.JOptionPane;
class FutureColatzCallback<V> extends FutureTask<V> {
public FutureColatzCallback(Callable<V> callable) {
super(callable);
}
public void done() {
String result = "Wynik: ";
if (isCancelled()) result += "Cancelled.";
else try {
result += get();
} catch(Exception exc) {
result += exc.toString();
}
JOptionPane.showMessageDialog(null, result);
System.exit(0);
}
}
/*The following iterative sequence is defined for the set of positive integers:
n → n/2 (n is even)
n → 3n + 1 (n is odd)
Using the rule above and starting with 13, we generate the following sequence:
13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1
It can be seen that this sequence (starting at 13 and finishing at 1) contains 10 terms. Although it has not been proved yet (Collatz Problem), it is thought that all starting numbers finish at 1.
Which starting number, under one million, produces the longest chain?
NOTE: Once the chain starts the terms are allowed to go above one million.
*/
public class Problem14 {
public static void main(String[] args) {
new Collatz();
}
}
No i teraz klasa jedno wątkowa:
public class NoProblem14 {
public static void main(String args[]){
final int MAX_NUMBER = 1000000;
int max = 0, step, maxStep = 0;
long temp;
long start,stop;
start = System.currentTimeMillis();
for(int i = 1; i <= MAX_NUMBER; i++) {
temp = i;
step = 0;
while( temp > 1) {
++step;
if ((temp % 2) == 0)
temp = temp/2;
else
temp = 3*temp+1;
if(step > maxStep ) {
max = i;
maxStep = step;
}
}
}
stop = System.currentTimeMillis();
String returnMessage = "Liczba to: "+max+ " ilość kroków " + maxStep + " wynik znaleziony w " + (stop - start) + " mils"; ;
System.out.println(returnMessage);
}
}
Program wielowątkowy u mnie wykonuje się w ok. 3400 ms, a jednowątkowy 260 ms.
<code=java>
podobają? :)