0

So I'm trying to familiarise myself with concurrent programing in java so I wrote the following code

public class Test{
    public static void main(String[] args){
        ForkJoinPool pool = new ForkJoinPool();
        pool.invoke(new Agent());

    }
}

class thread extends RecursiveTask<Integer>{    

    thread(){
        System.out.println("Thread");
    }

    public Integer compute(){ 
        thread x = new thread();
        x.fork();
        return 0;
    }

}

which as far as I understand should print 'Thread' infinitely but when I run it only print 'Thread' about 20 times.

Does anybody know why?

1
  • 3
    Naming your class lowercase t thread is a terrible pattern btw. MyRecursiveTask would be a better name. Commented Mar 22, 2014 at 17:50

1 Answer 1

1

The main thread will exit, and claim the live of the threads in the ForkJoinPool. You can see this with

public static void main(String[] args) 
{
    ForkJoinPool pool = new ForkJoinPool();
    pool.invoke(new MyRecursiveTask());

    try
    {
        Thread.sleep(10000);
    }
    catch (InterruptedException e)
    {
        e.printStackTrace();
    }
}

(Just for illustration, of course...)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.