Skip to content

Instantly share code, notes, and snippets.

@spullara
Created July 30, 2018 18:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spullara/6bfd070d1cd29f8f558a7360dbafe9eb to your computer and use it in GitHub Desktop.
Save spullara/6bfd070d1cd29f8f558a7360dbafe9eb to your computer and use it in GitHub Desktop.
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