multithreading - In Java's ForkJoinTask, does the order of fork/join matter? -


let's extended recursivetask called myrecursivetask.

then 2 sub-tasks created within scope of forkjointask:

myrecursivetask t1 = new myrecursivetask() myrecursivetask t2 = new myrecursivetask() t1.fork() t2.fork() 

i think "t2" @ top of workqueue (which deque, used stack worker itself), saw implementation fork method this:

public final forkjointask<v> fork() {     thread t;     if ((t = thread.currentthread()) instanceof forkjoinworkerthread)         ((forkjoinworkerthread)t).workqueue.push(this);     else         forkjoinpool.common.externalpush(this);     return this; } 

if so, there difference performance 2 expressions below:

expression1:

t1.join() + t2.join() 

expression2:

t2.join() + t1.join() 

i think may matter. t1.join() blocking (if there no work stealing) before t2.join() finished, because task @ top of workqueue can poped. (in other words, t2 has poped before t1 poped). below codes dojoin , tryunpush.

private int dojoin() {     int s; thread t; forkjoinworkerthread wt; forkjoinpool.workqueue w;     return (s = status) < 0 ? s :         ((t = thread.currentthread()) instanceof forkjoinworkerthread) ?         (w = (wt = (forkjoinworkerthread)t).workqueue).         tryunpush(this) && (s = doexec()) < 0 ? s :         wt.pool.awaitjoin(w, this, 0l) :         externalawaitdone(); }  /**  * pops given task if @ current top.  * (a shared version available via fjp.tryexternalunpush) */ final boolean tryunpush(forkjointask<?> t) {     forkjointask<?>[] a; int s;     if ((a = array) != null && (s = top) != base &&         u.compareandswapobject         (a, (((a.length - 1) & --s) << ashift) + abase, t, null)) {         u.putorderedint(this, qtop, s);         return true;     }     return false; } 

does have ideas this? thanks!

whether using java7 or java8 important. in java7 framework creates continuation threads join(). in java8 framework stalls join(). see here. i've been writing critique framework since 2010.

the recommendation using recursivetask (from javadoc):

return f2.compute() + f1.join(); 

this way splitting thread continues operation itself.

relying on f/j code direction not recommended since code changes frequently. instance, in java8 using nested parallel streams caused many compensation threads code reworked in java8u40 cause more problems. see here.

if must multiple joins, doesn't matter order join(). each fork() makes task available thread.


Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -