[concurrency-interest] ForkJoin updates

Doug Lea dl at cs.oswego.edu
Sun Mar 4 17:35:52 EST 2012

> The methods are a little annoying to use though because there are no "short"
> literals, so you have to say for example: if
> (task.compareAndSetForkJoinTaskTag((short)0, (short)1)) ... to conditionally
> add a simple mark. ...

> As for the API - in OutputStream for example, they provide the write() method
> using "int", but document them such that only the lowest byte is used:

In the bondage-and-discipline spirit of most of j.u.c :-),
I'd rather force the users of this API to explicitly acknowledge that
they are using a narrower-than-usual type. This seems OK mainly because
these tags are uninterpreted, and are made available so that thin layers
on top of FJT can be more time/space efficient than they could otherwise
be in the vastly most common case where there is only a small range of
possible tags. For example, those that internally translate enums that
are known to have only a few values:

enum Relationship { LIKES, FRIENDS, INVITES }
abstract class SocialNetworkGraphTask extends ForkJoinTask<Void> {
   void visit(Relationship r) {
      short tag = (short) (1 << r.ordinal());
      if (compareAndSetForkJoinTaskTag(..., tag))

More compelling use cases include various forms of completion-task
designs. I'll post more about these hopefully soon.

On the other hand, there is no reason to make common sensible
CAS-based idioms on tags any more tedious than necessary. So it seems
reasonable to at least provide a more streamlined way to add a value:
   short getAndAddToForkJoinTaskTag(short delta)


More information about the Concurrency-interest mailing list