java - Strange dead-lock with Streams API -


i working program, , decided use new streams api java 8. however, program stopped working when introduced .parallel(). here's relevant code:

import java.math.biginteger; import java.util.objects; import java.util.stream.stream;  import com.google.common.cache.*;  public class alg196 {      public static void main(string[] args) {         // add .parallel() suitable         long c = stream                 .iterate(biginteger.valueof(101), -> i.add(biginteger.one))                 .limit(100000000).map(biginteger::tostring).map(alg196::alg196)                 .filter(objects::nonnull).count();         system.err.println(c);     }      private static final string reverse(string n) {         return new stringbuilder(n).reverse().tostring();     }      private static final boolean ispalindrome(string s) {         (int = 0, j = s.length() - 1; < j; ++i, --j) {             if (s.charat(i) != s.charat(j))                 return false;         }         return true;     }      private static final string alg196(string n) {         system.err.println("processing " + n);         int loops = 0;         while (!ispalindrome(n)) {             n = new biginteger(n).add(new biginteger(reverse(n))).tostring();             loops++;             if (loops >= 100) {                 return null;             }         }         if (loops <= 10) {             return null;         }         return n;     } } 

the output contain many processing <x> lines when working correctly, never happens .parallel(). why this?

your program didn't stop - it's working hard trying generate requested range of bigintegers (in case 100000000) before submitting map tasks executors (try put breakpoint @ biginteger::add() method - , you'll see)

it's easy see thread dump

"forkjoinpool.commonpool-worker-2@710" daemon prio=5 tid=0xe nid=na runnable java.lang.thread.state: runnable @ java.util.stream.stream$1.next(stream.java:1033)   @ java.util.spliterators$iteratorspliterator.trysplit(spliterators.java:1784)   @ java.util.stream.abstractshortcircuittask.compute(abstractshortcircuittask.java:114)   @ java.util.concurrent.countedcompleter.exec(countedcompleter.java:731)   @ java.util.concurrent.forkjointask.doexec(forkjointask.java:289)   @ java.util.concurrent.forkjoinpool$workqueue.runtask(forkjoinpool.java:902)   @ java.util.concurrent.forkjoinpool.scan(forkjoinpool.java:1689)   @ java.util.concurrent.forkjoinpool.runworker(forkjoinpool.java:1644)   @ java.util.concurrent.forkjoinworkerthread.run(forkjoinworkerthread.java:157)` 

also please careful submitting many long-running tasks common forkjoin pool, may block threads in pool - can check thread (custom thread pool in java 8 parallel stream) solution


Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

order - Notification for user in user account opencart -