concurrency - java Fork/Join clarification about stack usage -
i read implementation of fork/join framework introduced in java 7 , wanted check understand how magic works.
as understand, when thread forks, creates subtasks in queue (which other thread may or may not steal). when thread attempts "join", checks queue existing tasks , recursively perform them, mean 'join' operation - 2 frames added thread call stack (one join , 1 new taken task invocation).
as know jvm not support tail call optimization (that may serve in situation remove join method stack frame) believe while performing complicated operation lot of forks , joins thread may throw stackoverflowerror
.
am right or did find cool way prevent it?
edit
here scenario clarifying question: (for simplicity) have 1 thread in forkjoin pool. @ point in time - thread forks , calls join. while in join method thread discovers can perform forked task (as found in queue) invokes next task. task in turn forks , call join - while executing join method thread find forked task in queue (like before) , invoke it. in stage call stack contain @ least frames 2 joins , 2 tasks.
as can see fork join framework transformed plain recursion. because java not support tail call optimization - every recursion in java can cause stackoverflowerror
if goes deep enough.
my question - did implementer of fork/join framework find cool way prevent situation.
there unfortunately nothing magical happening in terms of thread recursive stack. if initial task forks/splits , doesn't have reasonable resolution point run stackoverflowerrors.
you can understand why tutorial on javadoc splits each subtask in half.
Comments
Post a Comment