thread returning an object?

Discussion in 'Java' started by R, May 13, 2005.

  1. R

    R Guest

    Hello everybody.

    As a newbie I have a very simple question.

    Can a thread return a non void value? (run method is void...)

    e.g. an Object (LinedList, String[][]) ?

    thanks in advance for Your help
    best regards
    R
     
    R, May 13, 2005
    #1
    1. Advertising

  2. R

    Eric Sosman Guest

    R wrote:
    > Hello everybody.
    >
    > As a newbie I have a very simple question.
    >
    > Can a thread return a non void value? (run method is void...)
    >
    > e.g. an Object (LinedList, String[][]) ?


    The run() method is void, and cannot return anything.
    Even if it could, how would the caller know what to do
    with it? Note that the caller of run() is not your code,
    but something inside the JVM.

    All is not lost, though. Remember, you get to write
    the class that implements Runnable or extends Thread, and
    you can endow this class with any methods and fields you
    choose. Furthermore, objects of your class will not vanish
    simply because their threads finish executing; they're just
    like any other objects and will continue to exist as long
    as you have references to them. So if your object computes
    a "final answer," you can call your object's methods to
    retrieve that answer after the thread finishes. Provided
    you synchronize properly, you can even retrieve "preliminary
    answers" while the thread is still running.

    --
     
    Eric Sosman, May 13, 2005
    #2
    1. Advertising

  3. R

    Nigel Wade Guest

    R wrote:

    > Hello everybody.
    >
    > As a newbie I have a very simple question.
    >
    > Can a thread return a non void value? (run method is void...)
    >
    > e.g. an Object (LinedList, String[][]) ?
    >
    > thanks in advance for Your help
    > best regards
    > R


    Have a look at the implementation of SwingWorker at
    http://java.sun.com/docs/books/tutorial/uiswing/misc/example-1dot4/SwingWorker.java.

    I think this will show you how to do what you require. The getValue() and
    get() methods return an Object which is calculated by the SwingWorker
    thread.

    --
    Nigel Wade, System Administrator, Space Plasma Physics Group,
    University of Leicester, Leicester, LE1 7RH, UK
    E-mail :
    Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555
     
    Nigel Wade, May 16, 2005
    #3
  4. R

    ByteCoder Guest

    "R" <> wrote in news:1116004660.485881.9160
    @g14g2000cwa.googlegroups.com:

    > Hello everybody.
    >
    > As a newbie I have a very simple question.
    >
    > Can a thread return a non void value? (run method is void...)
    >
    > e.g. an Object (LinedList, String[][]) ?
    >
    > thanks in advance for Your help
    > best regards
    > R
    >


    You could let the Thread starting class implement an interface and when
    the thread is finished executing it would call the synchronized
    protected/public method with the object you want the thread to return.
    The thread starting class would do something with the object.

    --
    -------------
    - ByteCoder - ...I see stupid people
    -------------
    Curiosity *Skilled* the cat
     
    ByteCoder, May 16, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jim Macbeth
    Replies:
    3
    Views:
    529
    Jim Macbeth
    Nov 10, 2003
  2. Ken Godee

    returning a value from a thread

    Ken Godee, Jul 14, 2004, in forum: Python
    Replies:
    20
    Views:
    674
    Jeff Epler
    Jul 19, 2004
  3. Michael Hobbs
    Replies:
    0
    Views:
    320
    Michael Hobbs
    Jan 31, 2005
  4. Replies:
    11
    Views:
    681
    Christos Georgiou
    May 2, 2006
  5. Jean-Michel Pichavant

    Mocked object returning another Mocked object

    Jean-Michel Pichavant, Jul 13, 2012, in forum: Python
    Replies:
    1
    Views:
    208
    Ulrich Eckhardt
    Jul 13, 2012
Loading...

Share This Page