Guy Wald
Guy Wald

Reputation: 599

How can a Guava ListenableFuture<Object> wait for information from a Stack?

I have a private Stack S which is filled with objects from out-side of the class (using methods). A ListenableFuture should read the stack and retrieve an Object from it, but if the stack is empty it should wait for an object to be inserted to the stack and then retrieve it. I'm not sure how to implement this.

My idea was to use Wait / Notify for the ListenableFuture but is this correct logic (working with Guava)? What other options do I have?

Thanks in advance, Guy

Upvotes: 1

Views: 570

Answers (1)

Louis Wasserman
Louis Wasserman

Reputation: 198093

ListenableFuture and Guava don't come into this at all. The way to do this is to implement the stack with LinkedBlockingDeque, have the method to add elements to the stack use addFirst, and use pollFirst(long, TimeUnit) to wait the specified amount of time for an object to get inserted.

Never use low-level concurrency tools like wait and notify if you can do the same job with library support.

Upvotes: 7

Related Questions