private void invokeBody(CpsThread cur) { // prepare enough heads for all the bodies // the first one can reuse the current thread, but other ones need to create new heads // we want to do this first before starting body so that the order of heads preserve // natural ordering. // TODO give this javadocs worth a darn, because this is how we create parallel branches and the docs are cryptic as can be! // Also we need to double-check this logic because this might cause a failure of persistence FlowHead[] heads = new FlowHead[context.bodyInvokers.size()]; for (int i = 0; i < heads.length; i++) { heads[i] = i==0 ? cur.head : cur.head.fork(); } int idx=0; for (CpsBodyInvoker b : context.bodyInvokers) { // don't collect the first head, which is what we borrowed from our parent. FlowHead h = heads[idx]; b.launch(cur, h); context.bodyHeads.add(h.getId()); idx++; } context.bodyInvokers.clear(); }