I am trying to write code that has the following behavior:
- There are many simultaneous and random calls to X by various threads (Thread X)
- At some point in the future, one call is made to Y by one thread (Thread Y)
- Until Y is called, X must go through unchallenged, while X's simultaneous calls work fine
- After calling Y, any existing X calls should be terminated, but new X calls must be rejected in some way (RuntimeException, etc.)
- Y should not be executed until all existing calls to X are completed.
- Refresh . When Y is called, it should send a signal to objects that are running in Thread X, which tells them to abort () in an incorrect way (ideally ending very quickly so that Y can continue)
I looked at using Semaphore, CountDownLatch, and even recording my own AbstractQueuedSynchronizer, but none of them met the above requirements. For example, CountDownLatch assumes that you know how many calls to X will be made, which we do not know.
It seems like I almost need a mixture of CountUpDownLatch and maybe some kind of simple AtomicBoolean, but this is what I am using now, and from time to time I am at a dead end. Again, I use a sketchy-looking implementation of CountUpDownLatch, which is part of HSQLDB, which does not seriously look thread safe.
Any ideas on how I should approach this issue?
source
share