File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Using {RecursiveTask} in Java 1.7 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Using {RecursiveTask} in Java 1.7" Watch "Using {RecursiveTask} in Java 1.7" New topic
Forums: Java in General Features new in Java 7
Author

Using {RecursiveTask} in Java 1.7

Kevin Simonson
Ranch Hand

Joined: Oct 22, 2011
Posts: 103
I'm currently trying to learn how to use Fork and Join with Java 1.7. I started by taking a look at the web page at "http://www.oracle.com/technetwork/articles/java/fork-join-422606.html", and got as far as its mention of class {RecursiveTask}. At that point I thought I'd look {RecursiveTask} up at "http://docs/oracle.com/javase/7/docs/api", so I did, and noticed it gave an example of how to use {RecursiveTask} to calculate a Fibonacci number. I used its example, and just added a few more lines of code to come up with:

This works just fine when I execute "java Fibonacci 0" or "java Fibonacci 1", but when I try "java Fibonacci 2" I get the error message:

Exception in thread "main" java.lang.ClassCastException: java.lang.Thread cannot
be cast to java.util.concurrent.ForkJoinWorkerThread
at java.util.concurrent.ForkJoinTask.fork(ForkJoinTask.java:622)
at Fibonacci.compute(Fibonacci.java:21)
at Fibonacci.main(Fibonacci.java:31)

Can anybody tell me what I'm doing wrong?

Kevin S
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4174
    
  21

You are running the task from the main thread, you need to run it from a ForkJoinPool


Steve
Kevin Simonson
Ranch Hand

Joined: Oct 22, 2011
Posts: 103
Steve Luke wrote:You are running the task from the main thread, you need to run it from a ForkJoinPool

Thanks, Steve; that fixed it for me.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using {RecursiveTask} in Java 1.7