Created
July 30, 2018 18:48
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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