Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6656483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:38:57+00:00 2026-05-26T01:38:57+00:00

From Javadoc of ArrayBlockingQueue ArrayBlockingQueue : add public boolean add(E e) Inserts the specified

  • 0

From Javadoc of ArrayBlockingQueue ArrayBlockingQueue:

add

public boolean add(E e)

Inserts the specified element at the tail of this queue if it is possible 
to do so immediately without exceeding the queue's capacity, returning true 
upon success and throwing an IllegalStateException if this queue is full.

I always interpretted this statement (the part if it is possible to do so immediattely) as follows:

If the queue has free capacity, then the insert will succeed. If there is no empty space then it will not succeed.

But my understanding was wrong here.

In a simple case that I decided to use an ArrayBlockingQueue for e.g. 20 elements (small queue) and having one thread doing:

queue.take()

the other thread did not add an element to the queue via the add method despite the queue was almost empty.

I verified it also via debugging.

Once I replaced the call of queue.add(element) to queue.put(element) the element was indeed added to the queue.

So what is so different in these to methods?

For what other reason (besides capacity) could the addition not happen?


UPDATE:

public class ConnectionListener implements Observer {

  public static BlockingQueue<ConnectionObject> queueConnections = new   ArrayBlockingQueue<ConnectionObject>(10);

  @Override
  public void update(Observable arg0, Object arg1) {
      ConnectionObject con = ((ConnectionObject)arg1);
      queueConnections.add(con);
  }

}  

ConnectionObject is just a holder for String values.

public class ConnectionObject {
  private String user;  
  private String ip;
   //etc  
}

And the consumer:

public class ConnectionTreeUpdater extends Thread {  
  @Override
  public void run() {
    while(true){  
    try {  
    final ConnectionObject con = ConnectionListener.queueConnections.take();

If I use add no exception is thrown but element does not get added to the queue.

Just a thought: perhaps since the consumer is “waiting” on the queue, if for some internal housekeeping the element can not be added it will not be added and no exception is thrown.Could that be the case.

Otherwise I can not understand why there is no exception and with put the code works.

Are put and add meant to be used differently?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T01:38:57+00:00Added an answer on May 26, 2026 at 1:38 am

    It’s quite simple really:

    • if the queue is not full, both methods succeed;
    • if the queue is full, add() fails with an exception whereas put() blocks.

    I think the documentation is pretty clear on the above. If you don’t agree, and would like a second opinion, you could examine the source code for ArrayBlockingQueue:

    public boolean add(E e) {
        if (offer(e))
            return true;
        else
            throw new IllegalStateException("Queue full");
    }
    
    public boolean offer(E e) {
        if (e == null) throw new NullPointerException();
        final ReentrantLock lock = this.lock;
        lock.lock();
        try {
            if (count == items.length)
                return false;
            else {
                insert(e);
                return true;
            }
        } finally {
            lock.unlock();
        }
    }
    
    public void put(E e) throws InterruptedException {
        if (e == null) throw new NullPointerException();
        final E[] items = this.items;
        final ReentrantLock lock = this.lock;
        lock.lockInterruptibly();
        try {
            try {
                while (count == items.length)
                    notFull.await();
            } catch (InterruptedException ie) {
                notFull.signal(); // propagate to non-interrupted thread
                throw ie;
            }
            insert(e);
        } finally {
            lock.unlock();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

From the JavaDoc for setSoTimeout Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds. With
From the javadoc of Calendar.before(Object when) : Returns whether this Calendar represents a time
From time to time I see an enum like the following: [Flags] public enum
From what I've read about Windsor/Microkernel it is in theory possible to do everything
One thing I really, really miss from Javadoc is the ability to see which
From the JavaDoc, the EXTRA_STREAM parameter when launching an intent needs to be an
From the JavaDoc : returns: The String that is the result of evaluating the
I have read the following from Collator's Javadoc. The exact assignment of strengths to
From a web developer point of view, what changes are expected in the development
From a desktop application developer point of view, is there any difference between developing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.