What would be the best alternative for CountDownLatch ? In case we are using Java 4 and we want to implement a similar implementation of CountDownLatch.
If you are on Java 1.4 and are in need of a CountDownLatch type thing, then I suggest you update your product to use the current Java 8 JDK and use CountDownLatch.
Santosh Kumar Nayak wrote:...it is also interview question..
This is what should have been included in the original question you asked.
So basically, the interviewer has given you a "What-if" scenario and wants your opinion on the same. It has nothing to do with Java 1.4. Its just a question designed to see how you would think and come up with a solution. Can you share with us what was your answer so that we can assess that ?
I'm expecting something like this:
Interview question wrote:You might ordinarily use CountDownLatch to achieve X. How might you achieve X if CountDownLatch was unavailable?
This missing, but important, piece of information here is the definition of X. What are you trying to achieve?
Santosh Kumar Nayak wrote:The question was :-
What if you need an implementation like CountDownLatch and Java does not provide such functionality.What will you do ?
How can you achieve the same ?
My answer: Look around for an open-source implementation of something like CountDownLatch. In the Java environment it's very often easier to find somebody else's code rather than writing your own.
And by the way this would have worked back when Java 1.4 was king, because the people who designed Java 5's new concurrency classes first wrote Java code which worked with Java 1.4, and made it publicly available.
In an interview, personally, I would like to see the candidate understand the basics. How to share data? How to protect the data? How threads sends notifications to each other, in order to share data more efficiently... And a discussion regarding the possible implementation of a count down latch, where the shared data is a "release flag", and triggers the thread(s) behavior (group waking up) works well.
Or in other words, after you give an simple implementation overview, I would expect lots of followup questions too.