1

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


You should use something like :

@user = User.find(@id)
"#{@user.first_name} #{@user.last_name}"

(The second line returns what you want but I definitely advice you to create a method in the model)

4

1 に答える 1

7

ListenableFuture と Guava は、これにはまったく関与しません。これを行う方法は、 LinkedBlockingDeque を使用してスタックを実装し、スタックに要素を追加するメソッドでaddFirstを使用し、 pollFirst(long, TimeUnit) を使用して、オブジェクトが挿入されるまで指定された時間だけ待機することです。

ライブラリ サポートで同じジョブを実行できる場合は、待機や通知などの低レベルの同時実行ツールを使用しないでください。

于 2011-12-26T18:26:05.207 に答える