Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2005

Tip: Looking for answers? Try searching our database.

thread returning an object?

Thread view: 
R - 13 May 2005 18:17 GMT
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
Eric Sosman - 13 May 2005 18:33 GMT
> Hello everybody.
>
[quoted text clipped - 3 lines]
>
> 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.

Signature

Eric.Sosman@sun.com

Nigel Wade - 16 May 2005 09:44 GMT
> Hello everybody.
>
[quoted text clipped - 7 lines]
> 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.

Signature

Nigel Wade, System Administrator, Space Plasma Physics Group,
           University of Leicester, Leicester, LE1 7RH, UK
E-mail :    nmw@ion.le.ac.uk
Phone :     +44 (0)116 2523548, Fax : +44 (0)116 2523555

ByteCoder - 16 May 2005 18:54 GMT
"R" <ruthless@poczta.onet.pl> wrote in news:1116004660.485881.9160
@g14g2000cwa.googlegroups.com:

> Hello everybody.
>
[quoted text clipped - 7 lines]
> 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.

Signature

-------------
- ByteCoder -           ...I see stupid people
-------------
                  Curiosity *Skilled* the cat



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.