8 Replies Latest reply: Sep 20, 2011 12:32 AM by 836548 RSS

    rationale of ArrayBlockingQueue#add

    Johnny_hunter
      Hello all:

      I was practising the Producer-Consumer pattern and used BlockingQueue as the shared buffer. Firstly I used ArrayBlockingQueue#add() to populate the buffer, it throws IllegalStateException when it reached the capacity.

      By taking a peek into the source, I understand "add()" called "offer()": once offer() return false, it throws the exception. After I replaced add() with offer() the code went w/o exception.

      While I can understand how "add()" is implemented, I couldn't figure out the rationales. It appears to me this method contradicts the purpose of a BlockingQueue, which I thought is to block the execution of the running thread when the queue is full, and resume it otherwise; the client, however, doesn't need to be aware of this fact, at least not through a brutal exception.

      anyone knows in what scenario that the use of a "add()" method can be justified?

      Thanks,
      John
        • 1. Re: rationale of ArrayBlockingQueue#add
          DrClap
          The rationale for the existence of the add(E) method is simply that it must exist, since ArrayBlockingQueue<E> implements Collection<E> and the add(E) method is declared in that interface.

          And if you read the documentation for Collection#add, you'll see that its implementation in ArrayBlockingQueue fulfils that contract precisely. The documentation also explains why it is necessary to throw an exception.
          • 2. Re: rationale of ArrayBlockingQueue#add
            796440
            add() and offer() serve two different purposes. Sometimes, you want it to be an exceptional condition that changes the flow of control if you cannot enqueue something. Other times, you don't.
            • 3. Re: rationale of ArrayBlockingQueue#add
              836548
              Johnny_hunter wrote:
              Hello all:

              I was practising the Producer-Consumer pattern and used BlockingQueue as the shared buffer. Firstly I used ArrayBlockingQueue#add() to populate the buffer, it throws IllegalStateException when it reached the capacity.
              Yes it is behaving as per the doc. In doc it is clearly mentioned if the ArrayBlockingQueue capacity exceeds, it'll throw exception.

              >
              By taking a peek into the source, I understand "add()" called "offer()": once offer() return false, it throws the exception.
              add() won't call offer() by default, these are the two different methods. please go through the java docs.

              add() adds the element at the tail of the queue immediately. if capacity exceeds throws error.
              offer() also does the add of element in the tail, but won't throw exception, rather returns false.
              After I replaced add() with offer() the code went w/o exception.
              exactly this is what the java docs says. Have you checked whether the element was added in the queue? i am sure i was not.
              While I can understand how "add()" is implemented, I couldn't figure out the rationales. It appears to me this method contradicts the purpose of a BlockingQueue, which I thought is to block the execution of the running thread when the queue is full, and resume it otherwise; the client, however, doesn't need to be aware of this fact, at least not through a brutal exception.

              anyone knows in what scenario that the use of a "add()" method can be justified?
              If you want the exception should be throws in the code by default (without handling it manually using offer()) , whenever the capacity is exceeded, then go for add() method.

              >
              Thanks,
              John
              • 4. Re: rationale of ArrayBlockingQueue#add
                EJP
                add() won't call offer() by default, these are the two different methods.
                Bzzt. ArrayBlockingQueue.add() calls super.add() which is implemented in AbstractQueue.add() which calls offer().
                please go through the java docs.
                Please go through the Javadocs yourself. This is documented behaviour.
                • 5. Re: rationale of ArrayBlockingQueue#add
                  836548
                  EJP wrote:
                  add() won't call offer() by default, these are the two different methods.
                  Bzzt. ArrayBlockingQueue.add() calls super.add() which is implemented in AbstractQueue.add() which calls offer().
                  What is Bzzt???

                  Advice to use healthy language, we all are here to contribute. And mistakes happens with human, not with machine. If a made point is incorrect, there many way to put it in healthy way, rather than using such language (words).
                  • 6. Re: rationale of ArrayBlockingQueue#add
                    DrClap
                    833545 wrote:
                    EJP wrote:
                    add() won't call offer() by default, these are the two different methods.
                    Bzzt. ArrayBlockingQueue.add() calls super.add() which is implemented in AbstractQueue.add() which calls offer().
                    What is Bzzt???
                    It's the sound made on the TV game show when you give the wrong answer and get tossed out of the game.
                    Advice to use healthy language, we all are here to contribute. And mistakes happens with human, not with machine. If a made point is incorrect, there many way to put it in healthy way, rather than using such language (words).
                    • 7. Re: rationale of ArrayBlockingQueue#add
                      EJP
                      I must say I find this quite objectionable. As you don't know what 'bzzt' means, you have no idea whether it is 'healthy language' or not. I do not appreciate your presumptuous and sententious lecture. Stick to the technical topic.
                      • 8. Re: rationale of ArrayBlockingQueue#add
                        836548
                        EJP wrote:
                        Stick to the technical topic.
                        I'll appreciate that.