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.


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

Results 1 to 2 of 2

Thread: block and wakeup consumer threads/

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Thanked 0 Times in 0 Posts

    Default block and wakeup consumer threads/

    I have 2 threads.

    each of them reads some data from a shared buffer.

    currentDataBuffer.get(thisID); //currentDataBuffer is my shared buffer object

    I want to block thread after every call, and release it when all thread read the buffer (once)
    so I used this currentDataBuffer object as lock:

    synchronized (currentDataBuffer ) {

    The problem is how can I release those threads?

    inside currentDataBuffer I have a map where I store the ids of threads that read data from buffer.

    how can I use this.notifyAll(); (from currentDataBuffer ) to wake up all locked threads?
    Last edited by markiz; December 18th, 2011 at 11:18 AM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Eastern Florida
    Thanked 2,416 Times in 2,369 Posts

    Default Re: block and wakeup consumer threads/

    The syntax for notify & notifyAll is the same as for wait.
    Get the lock/monitor for the object and call notify. Only one thread will wake up and get the lock. The other waiting threads will have to be notified again and again until there are none left.

    Try writing a small simple program to exercise the wait/notify logic to see how they work.

Similar Threads

  1. [SOLVED] New to java, need to know how to incorparate system time into a block of code.
    By lostbit in forum Java Theory & Questions
    Replies: 5
    Last Post: September 29th, 2011, 07:46 AM
  2. try-catch block reports to have an Error that I don't know how to fix
    By baraka.programmer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 24th, 2011, 06:27 AM
  3. Java sliding block puzzle issues
    By Vesper in forum What's Wrong With My Code?
    Replies: 15
    Last Post: April 11th, 2011, 07:30 AM
  4. Creating Block cipher without the use of inbuilt java funcs
    By fortune2k in forum Algorithms & Recursion
    Replies: 0
    Last Post: November 15th, 2010, 09:43 AM
  5. Producer/Consumer Help
    By bananasplitkids in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 7th, 2010, 11:05 AM