thread method return value

Posted on 2005-04-29
Last Modified: 2010-03-31
After completion of running a thread is it possible force a return method from the run and return it back to the calling class?
Question by:PraKash
    LVL 86

    Expert Comment

    The calling class can call a method of a Runnable

    Runnable yourClass = new YourClass();
    new Thread(yourClass).start();
    Object o = yourClass.getReturnValue();
    LVL 86

    Accepted Solution

    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
    LVL 15

    Expert Comment

    If you are using Java 1.5
    or else you can use Doug Lee concurrent.util library (based on the same idea):
    LVL 86

    Expert Comment


    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    Suggested Solutions

    An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
    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…
    This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
    This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

    734 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

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now