wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes Thread to return a value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Thread to return a value" Watch "Thread to return a value" New topic
Author

Thread to return a value

Rajesh Agarwal
Ranch Hand

Joined: Aug 01, 2005
Posts: 79
Is it possible that a thread be made to return a value (Java Object) to the caller. If yes can you let me know how.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Hi

Welcome to JavaRanch!

A thread can't "return" a value in the normal sense that a method can, but what it can do is store a value somewhere, then notify other interested threads that the value has been stored. The value can be stored in a member of your Runnable implementation, or in any other well-known location -- for example, you could pass an object to the Runnable when it is constructed, and the Runnable can call a method on that object to report the result when it is completed.

To communicate between threads -- i.e., to send the message that the value is ready -- you'd use the Object.wait() and notify() methods -- do you understand those?


[Jess in Action][AskingGoodQuestions]
Rajesh Agarwal
Ranch Hand

Joined: Aug 01, 2005
Posts: 79
Originally posted by Ernest Friedman-Hill:
Hi

Welcome to JavaRanch!

A thread can't "return" a value in the normal sense that a method can, but what it can do is store a value somewhere, then notify other interested threads that the value has been stored. The value can be stored in a member of your Runnable implementation, or in any other well-known location -- for example, you could pass an object to the Runnable when it is constructed, and the Runnable can call a method on that object to report the result when it is completed.

To communicate between threads -- i.e., to send the message that the value is ready -- you'd use the Object.wait() and notify() methods -- do you understand those?
Rajesh Agarwal
Ranch Hand

Joined: Aug 01, 2005
Posts: 79
My basic requirement is that the main method calls 3 threads and each thread executes a stored proc and retrieves some results. Each result is then populated in some sort of a value object which needs to be returned to the main method. Now I dont want to synchronize this. How do I have these 3 threads return values to the main method.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Look into join() in the doc. You can do something like:

join() blocks until the other thread ends. So this could set two worker threads off to do some long running tasks and then wait for both to finish. It doesn't matter which one finishes first because we wait for both. Then we get the results from the worker threads.

Note that the repetition of doing 1 and then 2 gets old in a hurry. If you store your threads in a collection you could put the start calls into one loop and the join calls into another. Just be sure you start them all before you do the joins.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Rajesh Agarwal
Ranch Hand

Joined: Aug 01, 2005
Posts: 79
My Requirement is different but very simple. Dont understand how joins and all that can help.

The Main method of a class creates 3 threads, which is basically 3 different java classes. Each thread does some JDBC and executes some query, fetches the result, creates a Value Object. Now I want the three threads to return these 3 value objects to be returned back to the main method and use it in my main method.

Does anyone know.
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Yes, Stan James knows, and he told you one way to achieve what you want. Did you bother thinking about his post?


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Loveleen Saroya
Ranch Hand

Joined: Aug 08, 2005
Posts: 42
yes thread can return a value in java 1.5 , a new interface callable just like runnable which returns a value, but u can throw exceptions also
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Originally posted by Loveleen Saroya:
yes thread can return a value in java 1.5 , a new interface callable just like runnable which returns a value, but u can throw exceptions also


I don't understand this. But I rarely use Java 5.0/1.5 yet, so perhaps you can help.

I see the Callable interface. However, Callable is not Runnable and Thread can still only run Runnables, even in Java 5.0, can't it?
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3426
    
  12
Originally posted by Peter Chase:


I don't understand this. But I rarely use Java 5.0/1.5 yet, so perhaps you can help.

I see the Callable interface. However, Callable is not Runnable and Thread can still only run Runnables, even in Java 5.0, can't it?


You can create a Callable object from a Runnable object using ths Exexcutors class. I haven't actually tried this yet so i don't know whether it will achieve what the OP is after. Maybe (s)he can try it and let us know the result.


Joanne
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
You wind up getting a Future object back. It runs in the future and saves the results. After it has run you can get the result. It's all pretty cute. Sorry I don't have the details in front of me ... I think there were more in another thread.
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
OK, so there's this new "concurrent" package built in to the JDK, which provides additional features for multi-threading applications.

That does not mean that a Thread can return a value; it still can't.

What it means is that the JDK now provides the sorts of classes that previously people have written themselves or got from third-party libraries, to make it easier to do multi-threading. Obtainning a value from an asynchronous task, on its completion, is one of the things that such libraries, and now the JDK, make easier.

Or do I misunderstand? Does "concurrent" package do something more "magic" than that?
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
That sounds about right.

Just thought of another channel for returning values ... The concurrent package has a nice blocking queue. Producers of data (maybe many worker threads) put objects into the queue and a consumer (maybe the main thread) pulls them from the queue. The queue "blocks" so the consumer waits for new data if the queue happens to be empty.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread to return a value
 
Similar Threads
find out if there is a transaction associated with current thread?
Changing a variable inside a thread (and accessing it from the outside)
Servlet
Question with interrupt ()
wait and notify