Solved

FutureTask  run() method

Posted on 2012-03-18
4
357 Views
Last Modified: 2012-03-24
hi guys

From API

FutureTask.run()
          Sets this Future to the result of its computation unless it has been cancelled.

Can anyone give a simple example of what this means?

thanks
0
Comment
Question by:royjayd
  • 2
4 Comments
 
LVL 47

Expert Comment

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

http://programmingexamples.wikidot.com/futuretask

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

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

Author Comment

by:royjayd
ID: 37736351
Thanks!
The link doesnt answer my question. I want to know what run() does with a simple example.
0
 
LVL 35

Accepted Solution

by:
mccarl earned 350 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.
0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 150 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
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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:

713 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