On Apr 28, 2010, at 6:35 AM, Jacek Laskowski wrote:
On Fri, Jan 22, 2010 at 2:31 AM, David Blevins
<[email protected]> wrote:
In your test case you could construct a "CountDownLatch latch = new
CountDownLatch(1)" and pass that instance to your asynchronous
method so
both the test case and the target method have a reference to the
same latch.
The method will just call latch.await() which will cause the
asynch thread
to pause. Then in your test case you can test the 'isDone' logic on
your
future, then call latch.countDown() which will cause the asynch
thread to
resume and complete, then you can test your 'isDone' 'true' logic.
Best to
call get() on the future before calling isDone() or isDone() could
return
false.
Yay! David, that's awesome. I've never looked at the j.u.concurrent
package at all, but with the trick I felt in love with it. Really
great.
p.s. I once tried to get the gist of it, but since I didn't have
enough understanding of the concurrency package I failed. Now, it's
way clearer. I think the JDK team should take your comment and include
in the javadoc :-)
Glad you like it. The java.util.concurrent package has some really
great stuff.
If you haven't yet, check out the Atomic<Foo> classes. They're pretty
great also. Basically allows you to do things like "if the value is X
then set it to Y" all in one vm level operation which is super fast,
thread safe, and doesn't use any locks or synchronization.
-David