Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Alternative to CountDownLatch for use in Java 1.4  RSS feed

 
Santosh Kumar Nayak
Ranch Hand
Posts: 193
Eclipse IDE IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

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.
 
Tim Cooke
Marshal
Posts: 3872
233
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Santosh, you're focussing on the wrong problem. The problem is that you should not be using Java 1.4, which is now 15 years old.

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
Ranch Hand
Posts: 193
Eclipse IDE IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tim,

In case I do not want to use CountDownLatch then what is the option available for me.
(it is also interview question)
 
Tim Cooke
Marshal
Posts: 3872
233
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What problem are you trying to solve?
 
Campbell Ritchie
Marshal
Posts: 55715
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I log on to the Ranch, there is a picture of a hammer knocking in a screw. What you are asking is a bit like saying, “I have a hammer; what can I knock with it?” You shou‍ld be saying, “I want to do PQR; how can I do that?”
 
salvin francis
Bartender
Posts: 1601
35
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Tim Cooke
Marshal
Posts: 3872
233
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm more interested in what the question was.

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
Ranch Hand
Posts: 193
Eclipse IDE IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Tim Cooke
Marshal
Posts: 3872
233
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps the source code will help?
 
Paul Clapham
Sheriff
Posts: 22489
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another possibility is that the implementation of the count down latch is just an example for the interview.... meaning even if you memorized the implementation, it will likely not be enough.

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.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!