I’m trying to find a comparable code snippet for both Java and GPars to visualize, how much easier GPars is.
Does the following code do the same in both cases? I don’t mean the output only, but also what happens “inside”.
Or is there a way to shorten it even more?
Java:
package java;
public class Main extends Thread {
public void run() {
for (int i = 1; i <= 100; i++) {
System.out.println(i);
}
}
public static void main(String args[]) {
(new Main()).start();
}
}
Groovy:
import static groovyx.gpars.GParsPool.withPool
withPool{
for(int i=1; i<=100; i++) {
println (i)
}
}
The first example creates a new Thread, and runs a for loop inside it (the loop gets all the way through as the thread is not a daemon thread, so the VM will not exit until the thread completes)
The second example calls
withPool, then does nothing with this pool, and just runs the for loop in the current thread. You are not making use of any of the GPars concurrency methods, so thewithPoolcan be omitted for the exact same result.