Skip to content

Instantly share code, notes, and snippets.

@spullara spullara/App.java
Created Jul 30, 2018

Embed
What would you like to do?
Comparing Java Fibers vs ForkJoinPool - performance of FJP is 60% faster than Fibers are currently and use less memory
package com.sampullara.fibers;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.LongAdder;
public class App {
public static final int NUM = 100_000_000;
public static void main(String[] args) throws InterruptedException {
long start = System.currentTimeMillis();
LinkedBlockingDeque<Fiber> q = new LinkedBlockingDeque<>();
LongAdder la = new LongAdder();
Fiber.execute(
() -> {
for (int i = 0; i < NUM; i++) {
q.add(Fiber.execute(la::increment));
}
});
for (int i = 0; i < NUM; i++) {
q.poll(1, TimeUnit.MINUTES).await();
}
long diff = System.currentTimeMillis() - start;
System.out.println(la.longValue() / diff + " increments per ms");
}
}
package com.sampullara.fibers;
import java.util.concurrent.*;
import java.util.concurrent.atomic.LongAdder;
public class App2 {
public static final int NUM = 100_000_000;
public static void main(String[] args) throws InterruptedException, ExecutionException {
long start = System.currentTimeMillis();
LinkedBlockingDeque<ForkJoinTask> q = new LinkedBlockingDeque<>();
LongAdder la = new LongAdder();
ForkJoinPool.commonPool().execute(() -> {
for (int i = 0; i < NUM; i++) {
q.add(ForkJoinPool.commonPool().submit(la::increment));
}
});
for (int i = 0; i < NUM; i++) {
q.poll(1, TimeUnit.MINUTES).get();
}
long diff = System.currentTimeMillis() - start;
System.out.println(la.longValue() / diff + " increments per ms");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.