Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > thread returning an object?

Reply
Thread Tools

thread returning an object?

 
 
R
Guest
Posts: n/a
 
      05-13-2005
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

 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      05-13-2005


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.

--
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
 
 
 
Nigel Wade
Guest
Posts: n/a
 
      05-16-2005
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/tutor...ingWorker.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 : (E-Mail Removed)
Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555
 
Reply With Quote
 
ByteCoder
Guest
Posts: n/a
 
      05-16-2005
"R" <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
IsPostBack wrongly returning false (new thread) Bruno ASP .Net 0 01-05-2007 04:33 PM
returning none when it should be returning a list? randomtalk@gmail.com Python 11 05-02-2006 10:26 AM
Anyone else experience Thread.join() returning early? Michael Hobbs Python 0 01-31-2005 08:48 PM
returning a value from a thread Ken Godee Python 20 07-19-2004 03:59 PM
Worker Thread not running and is returning a status of stopped!!! Jim Macbeth ASP .Net 3 11-10-2003 09:34 AM



Advertisments