Difference between Execute , Submit and Invoke() in a ForkJoinPool
Asked Answered
P

1

17

I have got the following class which runs and compiles (you can try it out). The only thing puzzles me a little is that at the end in the main it works fine with fj.invoke(task) but it does not with fj.execute(task) and fj.submit(task). I don't get any output with the latter ones. From the API it should be working with the other methods too they perform the task.. even if they either return or not a value they should still perform the task. What am I missing here?

    import java.util.concurrent.RecursiveAction;
    import java.util.concurrent.ForkJoinPool;
    public class RecursiveTaskActionThing extends RecursiveAction{
         int roba;
        static int counter;
        public RecursiveTaskActionThing(int roba)
    {
        this.roba = roba;

    }
        public void compute()
    {
        if (roba<100)
        {
            System.out.println("The thing has been split as expected: "+ ++counter );
        }
        else{

                roba = roba/2;
                RecursiveTaskActionThing rc1 = new RecursiveTaskActionThing(roba);
                RecursiveTaskActionThing rc2 = new RecursiveTaskActionThing(roba);
                this.invokeAll(rc1,rc2);

        }

        }
        public static void main (String []args)
        {
            ForkJoinPool fj = new ForkJoinPool();
            fj.invoke(new RecursiveTaskActionThing(500));
}
}

You can try it out simply by copy and paste the code, by replacing

fj.invoke(new RecursiveTaskActionThing(500)); with

fj.execute(new RecursiveTaskActionThing(500)); or with

fj.submit(new RecursiveTaskActionThing(500)); it won't spit out any output... I am wondering why.

Thanks in advance.

Pirandello answered 26/7, 2013 at 12:21 Comment(0)
B
25

invoke will execute and join on that task. execute and submit will push the task to a work queue to be worked on later. If you want to see the expected output call the join method of the task after submiting or executeing.

Now that final question should be, 'Why won't the task run at all?' The threads are created as setDaemon(true), and so when you leave your main method you main Thread dies. And as the spec specifies when only daemon threads are running the system will exit.

By joining on the task, you are suspending the main thread until the fork join tasks complete.


Based on your last question, it would really be beneficial to you to learn how to read other code for specific questions like this.

Belita answered 26/7, 2013 at 12:56 Comment(4)
Great, thanks a lot, just a curiosity do you work with this stuff on a regular basis or you are just passionate?Pirandello
@Pirandello just passionate :)Belita
is the main thread always set to false?. I ran code and try to set Deamon as true after few lines of main method. Then i see exception "lIlegalThreadStateException" throwing at the same line where I put Thread.currenthread().setDeamon(true). If main thread is false then it meant to be User thread. Correct me, please?Derosier
@Derosier By definition, if the main thread were daemon then the application will not run. The main thread will always be the first thread run (which means the only thread run). If it were daemon then it would immediately be killed. So to answer your question, the main thread is not a daemon thread. The IllegalStateException you see is because the daemon flag must be set before the thread is started. By setting it as currentThread().setDaemon(true) the thread is already running and will fail.Belita

© 2022 - 2024 — McMap. All rights reserved.