Skip to content

Instantly share code, notes, and snippets.

@travisdowns
Created April 16, 2018 19:45
Show Gist options
  • Save travisdowns/fae65e9fe4f8905141aea17b836d8db5 to your computer and use it in GitHub Desktop.
Save travisdowns/fae65e9fe4f8905141aea17b836d8db5 to your computer and use it in GitHub Desktop.
package stackoverflow;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadLocalRandom;
import java.util.function.DoubleUnaryOperator;
import java.util.stream.DoubleStream;
public class TestCalculate {
private Random rnd = ThreadLocalRandom.current();
private DoubleStream randomPoints(long points, double a, double b) {
return rnd.doubles(points)
.limit(points)
.map(d -> a + d * (b - a));
}
public static void main(String[] args) throws Exception {
DoubleUnaryOperator du = x -> (x * Math.sqrt(23.35 * x * x) / Math.sqrt(34.54653324234324 * x) / Math.sqrt(213.3123)) * Math.sqrt(1992.34513213124 / x) / 88392.3 * x + 3.234324;
for (int i=0 ; i < 2; i++){
int j = i ;
CountDownLatch latch = new CountDownLatch(3);
new Thread(() -> {
TestCalculate test = new TestCalculate();
int x = 0;
System.out.println("Thread "+j+" start");
long start = System.currentTimeMillis();
while (x++ < 6) {
double d = test.randomPoints(50_000_000l, 2, 10).map(du).sum();
double end = (System.currentTimeMillis() - start) / 1000.0;
System.out.println(String.format("Thread %d: time: %5.2f seconds, result: %f",
j, end, d));
latch.countDown();
start = System.currentTimeMillis();
}
System.out.println("Thread "+j+" stop");
}).start();
latch.await();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment