Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

how to use a Task? get something from the future

 
Rancher
Posts: 259
13
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i created a task and it's supposed to return a File, but i can't get it to return anything. the task works fine if i set the return type to void, but i really need it to return a File.
i think maybe it's a problem with how i'm using FUTURE .get() ???

 
Marshal
Posts: 27374
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not too familiar with the submit(Runnable, T) method but here's how I see it. The documentation says:



To me that says "You pass a T object to the submit method, your Runnable object does it's thing and modifies the T object, then you get the T object back from the Future eventually."

But you didn't pass a T object, you passed null. And presumably your runnable object is off creating and filling up some file, how it knows what file to use I can't tell from your post, but there's no way for the submit method to return that file. Instead you're getting the one you passed in, which was null.

I presume you're getting a NullPointerException? You didn't say anything about the symptoms of your problem.

I think you probably need to use the submit(Callable<File>) method instead. It returns a Future<File> object and it fits better with the idea that the Callable is going to produce a file for you without being told what file to produce.
 
S Fox
Rancher
Posts: 259
13
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when you click a button, a file-chooser opens and lets you pick a text file to use as the inputFile.
then i'm submitting a task to executorservice which acts on that inputFile

if i do: Future<File> future1 = es.submit(rft);
it says: Type mismatch: cannot convert from Future<capture#1-of ?> to Future<File>

i don't understand why i get a type mismatch, but that's why i thought i needed to do:
Future<File> future1 = es.submit(rft, result);

here's my method for calling the task which looks more correct, but it still doesn't get back a file.
when i try to print the size, the method freezes there. but there's no error on the console.
the file clearly doesn't exist but it's not throwing a NullPointerException.



here's my task. it removes unwanted chars from a text file, converts to uppercase, and saves the result as *_out.txt
i'm pretty sure this class is all correct, unless it needs to implement Callable?
 
S Fox
Rancher
Posts: 259
13
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i figured it out, here's the correct way:



i didn't need to create a Future<File> object at all. i just had to use the get method on my Task.
 
Slideshow boring ... losing consciousness ... just gonna take a quick nap on this tiny ad ...
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic