What should I use to get semantics equivalent to AutoResetEvent in Java? (See this question for ManualResetEvent).
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
@user249654's answer looked promising. I added some unit tests to verify it, and indeed it works as expected.
I also added an overload of
waitOne
that takes a timeout.The code is here in case anyone else finds it useful:
Unit Test
AutoResetEvent with overload that accepts a timeout
I believe what you're looking for is either a CyclicBarrier or a CountDownLatch.
One more extension to the solution from the accepted answer in case you would like to know whether your wait finished with timeout or with event set (which is exactly what .NET AutoResetEvent does).
I was able to get CyclicBarrier to work for my purposes.
Here is the C# code I was trying to reproduce in Java (it's just a demonstration program I wrote to isolate the paradigm, I now use it in C# programs I write to generate video in real time, to provide accurate control of the frame rate):
and here is the Java code I came up with to do the same thing (using the CyclicBarrier class as suggested in a previous answer):