• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Using {RecursiveTask} in Java 1.7

 
Ranch Hand
Posts: 244
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are running the task from the main thread, you need to run it from a ForkJoinPool
 
Kevin Simonson
Ranch Hand
Posts: 244
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 didn't like the taste of tongue and it didn't like the taste of me. I will now try this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic