FutureTask  run() method

Posted on 2012-03-18
Medium Priority
Last Modified: 2012-03-24
hi guys

From API

          Sets this Future to the result of its computation unless it has been cancelled.

Can anyone give a simple example of what this means?

Question by:royjayd
  • 2
LVL 47

Expert Comment

ID: 37736318
This is a simple example of the code showing how FutureTask is working:


click on showcosde:
Example of FutureTask
+ Show Code  <--- click here

IThe code is reallytransparent and in compliance with the words in API.

Author Comment

ID: 37736351
The link doesnt answer my question. I want to know what run() does with a simple example.
LVL 36

Accepted Solution

mccarl earned 1400 total points
ID: 37736357
I agree the words in the Javadoc for that method are quite hard to understand without deeply understanding what is happening.

However, I can't really give an example of that method because user code would never call that method. That method is there so that when you call execute() method on some ExecutorService (as an example) with this FutureTask as the argument, the internals of the execute() call is actually what calls the FutureTask's run() method for you.

What FutureTask's run() method would do, is it would call either the run() method or the call() method of the Runnable/Callable that is passed in to FutureTask as the constructor argument.
LVL 47

Assisted Solution

for_yan earned 600 total points
ID: 37736368
You'll not call this method yourself, that's what it says in the API:
This method is invoked internally by the run method upon successful completion of the computation.

ANd the way you use FuturTask that example shows very convincingly

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question