• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread to return a value

 
Rajesh Agarwal
Ranch Hand
Posts: 79
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 79
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 79
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rajesh Agarwal
Ranch Hand
Posts: 79
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1970
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, Stan James knows, and he told you one way to achieve what you want. Did you bother thinking about his post?
 
Loveleen Saroya
Ranch Hand
Posts: 42
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1970
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 3742
16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1970
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic