[concurrency-interest] Single-threaded ForkJoinPool

Peter Levart peter.levart at gmail.com
Sat Feb 6 04:24:07 EST 2016


Hi Gili,

Have you thought of using SplittableRandom instead? Initially you create 
it with a predefined seed for the root task. Whenever you fork-off a 
task, you create for it a new instance by invoking 
SplittableRandom.split(). This should give you deterministic behavior 
regardless of how many threads you use for fork-join pool and the 
dynamics of execution. For example:

public class SumRandomInts extends RecursiveTask<Long> {
     final SplittableRandom rnd;
     final int count;

     public SumRandomInts(int count, SplittableRandom rnd) {
         this.rnd = rnd;
         this.count = count;
     }

     @Override
     protected Long compute() {
         if (count < 1000) {
             long sum = 0L;
             for (int i = 0; i < count; i++) {
                 sum += rnd.nextInt();
             }
             return sum;
         } else {
             SumRandomInts t1 = new SumRandomInts(count / 2, rnd.split());
             SumRandomInts t2 = new SumRandomInts(count - count / 2, 
rnd.split());
             t1.fork();
             return t2.compute() + t1.join();
         }
     }

     public static void main(String[] args) throws Exception {
         long result = ForkJoinPool.commonPool().submit(
             new SumRandomInts(100000, new SplittableRandom(12345L))).get();
         System.out.println(result);
     }
}


Regards, Peter

On 02/06/2016 04:18 AM, cowwoc wrote:
> Hi,
>
> Is this the correct mailing list for discussing ForkJoinPool in JDK9? If
> not, please point me to the right place.
>
> I have a feature request for ForkJoinPool which doesn't seem to be possible
> to implement without a JDK change: http://stackoverflow.com/q/34012134/14731
>
> Specifically, I need to be able to an application that uses Random and
> ForkJoinPool in a deterministic manner when debugging/profiling but run
> full-speed in normal execution mode. I have all the moving parts nailing
> down except for ForkJoinPool.
>
> If I create ForkJoinPool with a parallelism of 1, sometimes I see two worker
> threads getting used. I am guessing that this is caused by
> ForkJoinTask.get() invoking ForkJoinPool.common.externalHelpComplete(), but
> maybe something else is going on.
>
> Is there a way for me to guarantee that ForkJoinThread will use exactly 1
> worker thread, no less, no more? Would you like me to file a formal feature
> request?
>
> Thank you,
> Gili
>
>
>
> --
> View this message in context: http://jsr166-concurrency.10961.n7.nabble.com/Single-threaded-ForkJoinPool-tp13232.html
> Sent from the JSR166 Concurrency mailing list archive at Nabble.com.
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at cs.oswego.edu
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20160206/dd69b15e/attachment.html>


More information about the Concurrency-interest mailing list