Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 18 of 18

Thread: CountDownLatch, Semaphores and Reentrant Locks

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default CountDownLatch, Semaphores and Reentrant Locks

    1. how to add a constructor to initialise the latch.
    2. add a constructor to initialise the latch and the lock.
    3. add a constructor to initialise the semaphore and lock
    Also, how can i inform the Sample A thread that polls from Sample B are finished using the latch in part (1)/(2) and semaphore in part (3).
    Thanks


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: CountDownLatch, Semaphores and Reentrant Locks

    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: CountDownLatch, Semaphores and Reentrant Locks

    Can you post the code that shows the problem and ask some questions about the problems you are having?

    Be sure to wrap the code in code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: CountDownLatch, Semaphores and Reentrant Locks

    Please post the code that shows the problems you are having.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: CountDownLatch, Semaphores and Reentrant Locks

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    Where is your code for where there are TODO comments?
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: CountDownLatch, Semaphores and Reentrant Locks

    unsure if the codes are correct. For (1)
    The first test is: does the code compile?
    The next is: does the code do what you want?

    What are the answers to those questions?

    NOTE: The posted code is poorly formatted. One statement per line makes it easier to read and understand.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: CountDownLatch, Semaphores and Reentrant Locks

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    Do you have any questions about or problems with the code in post#11?
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: CountDownLatch, Semaphores and Reentrant Locks

    You missed this part of my post:

    Do you have any questions about or problems with the code in post#11?
    Please ask specific questions.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: CountDownLatch, Semaphores and Reentrant Locks

    would like to know what is the exact code to add in the TODO areas of the codes.
    Take each of the TODO areas one at a time and ask some specific questions about the problems you are having with it.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: CountDownLatch, Semaphores and Reentrant Locks

    What is the purpose of the CountDownLatch object? How will the code use it to control the code's execution?
    Will the CountDownLatch objects be created in the Newspaper objects or in the Election object?
    See the CountDownLatch class's API doc for an idea.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: CountDownLatch, Semaphores and Reentrant Locks

    used to control results of polls
    I guess I'm not sure what the assignment is supposed to do.
    This statement:
    ensure that the election results are not published until all the polls are in.
    implies to me that the CDL should prevent the results from being published until AFTER all newspapers have finished polling. That means that there needs to be coordination between the newspapers. How will that be done using a CDL?
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: CountDownLatch, Semaphores and Reentrant Locks

    Is this the programming problem?
    How will the CDL be used to make sure the newspapers don't publish their results until all newspapers have finished their polling?
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: CountDownLatch, Semaphores and Reentrant Locks

    How can the CDL be used to make sure all results are in BEFORE any results are printed?
    Each newspaper needs to wait until all the newspapers are finished polling.

    Did you read the example in the API doc for the CDL class?

    I have run the program.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: CountDownLatch, Semaphores and Reentrant Locks

    I'll help you get your code to work after you've fully described how the CDL is supposed to be used.
    The first question is: where should the CDL object(s) be defined? Is there one or more that one?
    Which methods of the CDL class will be used and how?
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Jun 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: CountDownLatch, Semaphores and Reentrant Locks

    Please I need a solution for this question. How do you
    </*	TODO (b) add a constructor to initialize the latch and the lock */>

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: CountDownLatch, Semaphores and Reentrant Locks

    Sorry, I don't write solutions for students.

    Try to answer the questions in post#26. If you don't understand any of the questions, post the question and ask your questions about it.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: CountDownLatch, Semaphores and Reentrant Locks

    methods of CDL to be used in this context are the incremental await and signal
    Where are you reading the API doc? I don't see a signal() method in the API doc I'm looking at:
    Java Platform SE 7

    Where will the CDL object's constructor be called to create a new CDL object? There are two classes.
    If the newspaper objects are to use the same CDL, how will they get references to it? See the code example in the API doc.

    threads of Election results to wait while threads of newspaper results
    That is confusing. There is only one thread that uses the Election class's method and that is in the main() method.
    The CDL is used to force the threads in the newspaper objects to wait until all the results have been gathered by the polling.
    To wait the threads need to call the await() method.
    To tell everyone that its polling is done, the thread needs to call the couuntd0wn method.
    If you don't understand my answer, don't ignore it, ask a question.

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: CountDownLatch, Semaphores and Reentrant Locks

    I've made simple versions of those classes for testing. They are not really needed yet.

    Continue working on how to use the CDL to make the NP wait for the end of polling.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Implementing Semaphores
    By bananasplitkids in forum Threads
    Replies: 1
    Last Post: March 27th, 2010, 04:35 AM