In my program, I am essentially trying to connect to a publisher and get data. The basic functionality is there in these steps
- I make the connection to the publisher with username and password etc
- I make the request for data. Method exits
- The publisher’s API gives me a callback to a method
onDataUpdate(Object theUpdate)
From there, I can print the data, or write it to a database or anything I need to do. That all works.
My problem is, I would now like to wrap the functionality in such a way that a calling program can say request the data and receive it as soon as I have it. Meaning, I want my exposed method to look like
public Object getData() {
subscribeForData();
// somehow wait
return theUpdate;
}
How can I make this happen? Is there some way I can use threads to wait/notify when I’ve received the update? I’m a newb to stackoverflow and also multithreaded programming, so any help and sample code would be much appreciated!! Thanks in advance.
In this case I would prefer to use CountDownLatch, where i’ll initialize my
lathchwith count 1 as soon i subscribe for publisher i will callawait() onlatchand when i get the callback i’llcountdownthelatch.