Wait and Notify in Java threads for a given interval

541 views Asked by At

i am working on a usecase as below. I am new to multi threading and facing this issue with using it.

  1. I broadcast a event on network.
  2. Its received by all the listeners, and they unicast me with their information.
  3. This is received inside the call back method as below, i will get N unknown number of callback threads. depending on listeners at that particular time.
  4. I have to collect a list of all subscribers.

I have to wait at least 10sec for all the subscribers to reply to me.

        //Sender

            public void sendMulticastEvent() {
                api.sendEvent();
                /* after sending event wait for 15 sec so call back can collect all the subscribers */
                //start waiting now
            }


    //Callback method
            public void receiveEventsCallback(final Event event) {
                //i will receive multiple response threads here..  
                //event object will have the topic and subscribers details, which i will collect here
               list.add(event)

               notify()
               //notify thread here so i have a cumulative list of all received events.
            }

I am only concerned on How to.. ?

  1. Start a wait at the sendMulticast event for X seconds
  2. Notify at receiveEventsCallback() after all the recieved events has been added to the list.

I have read theroitically on wait and notify, Countdownlatch and Barrier. But i am not sure which would be good, because of my poor experience in multithreading.

2

There are 2 answers

1
Tsyvarev On BEST ANSWER
  1. Start a wait at the sendMulticast event for X seconds

Just use version of wait() which takes timeout argument.

Note, that you should manually update timeout value after every successfull wait() call (that is, which return event).

  1. Notify at receiveEventsCallback() after all the recieved events has been added to the list.

Your question insists that you don't know, how many listeners in your network. How can you know, that all of them have event recieved (and replied)?

The only way for sender is to wait X second and process all replies available till that moment.

1
Peter Szabo On

If you know how many replies you will get - assuming each response will trigger the creation of a new thread - use a CyclicBarrier.

https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CyclicBarrier.html

example:

CyclicBarrier barrier = new CyclicBarrier(3);

    Runnable thread = new Runnable()
    {
            @Override
            public void run()
            {
                    try
                    {
                            barrier.await();
                            for (int i = 0; i < 10; i++)
                            {
                                    System.out.printf("%d%n", i);
                            }
                    }
                    catch (InterruptedException | BrokenBarrierException ex)
                    {
                            ex.printStackTrace();
                            // handle the exception properly in real code.
                    }

            }
    };

Untill the third barrier.await() each thread will wait.