user1137146
user1137146

Reputation: 285

Callback to parent thread from child thread

I've got a parent thread. It's job is to queue request and init child thread. Child thread is supposed to get data from web service and callback to parent thread. Parent thread process data and start another child thread. The problem is that everything which is inside callback method is handled by the child thread not the parent thread. Is there a way to handle callback by parent thread? In other words the child thread finishes right after calling the callback method without processing it.

Upvotes: 2

Views: 1503

Answers (2)

amicngh
amicngh

Reputation: 7899

As Marko mentioned you can use ExecutorService which have Future reference there you can return result of running job to its parent thread.

Upvotes: 0

Marko Topolnik
Marko Topolnik

Reputation: 200168

You need some design for that, but for starters I don't see the point of a child thread if the main thread won't do anything but wait for the child to complete. If, on the other hand, the main thread must serve further incoming requests, then it can't also serve the callbacks from child threads. You'll have to rethink that. One idea is for one thread to collect requests and push them to a queue. Child threads push their events to the same queue and there is a separate thread that processes events from the queue -- both the requests and responses from child threads.

If such a thing sounds good to you, then by all means use the ExecutorService to handle the queuing of jobs.

Upvotes: 2

Related Questions