Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 205
  • Last Modified:

thread method return value

After completion of running a thread is it possible force a return method from the run and return it back to the calling class?
0
PraKash
Asked:
PraKash
  • 3
1 Solution
 
CEHJCommented:
The calling class can call a method of a Runnable

Runnable yourClass = new YourClass();
new Thread(yourClass).start();
yourClass.join();
Object o = yourClass.getReturnValue();
0
 
CEHJCommented:
An alternative would be to have YourClass call back the calling class

public void run() {
    // do it
    caller.setValue(x); // done it, so make callback
}
0
 
aozarovCommented:
If you are using Java 1.5 http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/Future.html
or else you can use Doug Lee concurrent.util library (based on the same idea): http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/FutureResult.html
0
 
CEHJCommented:
:-)
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now