Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8994675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:29:08+00:00 2026-06-15T23:29:08+00:00

I am testing the Fibonacci example using RecursiveTask in Java SE 7 http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/RecursiveTask.html .

  • 0

I am testing the Fibonacci example using RecursiveTask in Java SE 7 http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/RecursiveTask.html.

The program is as follows:

import java.util.concurrent.*;

public class testfuture{
    public static void main(String[] args) {
        System.out.println("Hello, World");
        Fibonacci fib = new Fibonacci(10);
        int result = fib.compute();
        System.out.println(result);
        }
}

class Fibonacci extends RecursiveTask<Integer> {
    final int n;
    Fibonacci(int n) { this.n = n; }
    public Integer compute() {
        if (n <= 1)
        return n;
        Fibonacci f1 = new Fibonacci(n - 1);
        f1.fork();
        Fibonacci f2 = new Fibonacci(n - 2);
        return f2.invoke() + f1.join();
    }
}

However, the program throws a run-time exception

Hello, World
Exception in thread "main" java.lang.ClassCastException: java.lang.Thread cannot be cast to java.util.concurrent.ForkJoinWorkerThread
    at java.util.concurrent.ForkJoinTask.fork(Unknown Source)
    at Fibonacci.compute(testfuture.java:21)
    at testfuture.main(testfuture.java:9)

I googled about this issue but could not figure out the problem.

Thanks for your help.

================

Solution:

public class testfuture{
    public static void main(String[] args) {
        System.out.println("Hello, World");
        Fibonacci fib = new Fibonacci(10);
        ForkJoinPool pool = new ForkJoinPool();
        int result = pool.invoke(fib);
        //int result = fib.compute(); //run-time exception
        System.out.println(result);
        }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T23:29:09+00:00Added an answer on June 15, 2026 at 11:29 pm

    You’re misusing ForkJoinTask.

    The point of ForkJoinTasks is to execute them within a ForkJoinPool.
    The pool will call the compute() methods of the tasks for you in its ForkJoinWorkerThreads.

    You should not call compute() directly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Testing the Javascript Implementation of MD5 here: http://www.webtoolkit.info/javascript-md5.html gives the following output: MD5(muzaaya) =
Testing a GUI with the Run Button, there is an Error: java.lang.ClassNotFoundException: com.jgoodies.looks.plastic.Plastic3DLookAndFeel at
While testing my site (www.luchtspin.nl) with the Utilu IE Collection (IE6-8 - http://utilu.com/IECollection )
Testing Your Expansion Files says: ... For example, if your package name is com.example.android,
For testing I'm using the H2 database . For production it's MySQL . I
//Testing numbers for primality #include <iostream> #include <cmath> using namespace std; int main() {
Testing environment: netbeans7.11 + glassfish 3.11 here is the jsp, class code: <%@page contentType=text/html
When testing my app using Instruments -> Activity Monitor, I'm seeing a difference in
When testing scipy using the nose package using scipy.test() , the test fails under
Testing tool for developers Graph Api Explorer, when I make the following FQL query:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.